Skip to main content

Posts

Showing posts with the label Batch File

PowerShell switch case call function

Code below collect user input and uses switch case statement to check if the input matches, and if it matches a function is called that will execute commands.Here’s the code:#get or read from user input$computer_name=read-host ("Enter Computer Name:")switch ($computer_name ){#if the input is computer_1 then function func_comuputer1 is called computer_1{func_computer1}computer_2{func_computer2}}#function called if computer_1 is the inputfunctionfunc_computer1 {write-host"You entered Computer_1"#or replaced with other function like reboot / shutdown /or other commands#Restart-Computer -ComputerName computer_1}
#function called if computer_2 is the inputfunctionfunc_computer2 {write-host"You entered Computer_2"#or replaced with other function like reboot / shutdown /or other commands#Ex: Enter-Pssession computer_2}

Code below will keep asking for input and will only exit if a specific string matches to exit the script. Here’s the code:do {$computer_name

PowerShell launch an application and close it automatically

Code below will run an application and PowerShell will also close it after a few seconds.$process_id=Invoke-CimMethodWin32_Process-MethodName"Create"-Arguments @{CommandLine ='cmd /c "C:\dev\remind.bat"'} |Select-Objectprocessid|ft-HideTableHeaders|Out-StringStart-Sleep-Seconds2.5stop-process$process_idSample contents of remind.bat:
@echo off
echo "Shutdown the server. Do it now."
Notepad
==================Notepad is on the batch file so the command window will stay on the top of any running applications.Sample output of the above code:
Code below will run calculator and close after a few seconds specified on the code:Invoke-CimMethodWin32_Process-MethodName"Create"-Arguments @{CommandLine ='calc.exe'} |Select-Objectprocessid|ft-HideTableHeaders|Out-StringStart-Sleep-Seconds2.5$get_process_id=Get-Process-Namecalcu*|<

Get PowerShell version on remote computers

PowerShell version on remote computers must be installed with the latest version if possible. It would make life easier.
New version comes with of course, new features and more fun. And if all servers or PCs has the same PowerShell version, then you need to create one version of PowerShell that works for all computers or servers.
But how to get PS version on remote computers?
To get the version locally, it’s quite straight forward:
Method 1: ($Psversiontable).PSVersion
Method 2:
(get-host).Version
To get version on remote computers, just add a few lines from the command above.

#--------- save as Get_PS_Version.ps1 or any file name $xversion= (get-host).Version $xversion_output="PowerShell Version: "+$xversion
$pc_name=$env:computername $pc_output="Computer Name: "+$pc_name

$xversion_output+"`n">>c:\scripts\output\$pc_name.txt $pc_output>>c:\scripts\output\$pc_name

How to check office version from command line

The are quite a few ways to check office version it can be done via registry, PowerShell or VBScript and of course, good old command line can also do it.
Checking Windows office version whether it is Office 2010, Office, 2013, Office 2016 or other version is quite important to check compatibility of documents; or just a part of software inventory.
For PowerShell this simple snippet can check the office version:
$ol=New-Object-ComObjectExcel.Application $ol.Version
The command line option will tell you where’s the path located; the result will also tell whether office is 32-bit, 64-bit and of course the version of the office as well.
Here’s the command that will check the office version and which program directory the file is located which will tell whether it’s 32-bit or 64-bit.
Command to search for Excel.exe:
DIR C:\ /s excel.exe | find/i "Directory of" 
Above command assumes that program files is on  C: drive.
Sample Output, if 64-bit version:
Directory of C:\Program Files\…

Copy or migrate shared folders to another server

Migrating or copying files to another server and retaining the permissions is a common task when migrating a file server.
If all permissions are successfully retained it will make the migration seamless and nobody will ever notice that a migration has taken place.
If there are shared folders and with different permissions, re-sharing the folder by scratch is just time consuming and giving access denied to users will be inevitable.
But how to copy files and folders, like it was exactly done on the old server?
In Windows environment, just 3 steps are needed. 3 steps sound easy and quick.
Steps below will work for NTFS permissions and folder access rights solely depends on it.
a.Copy the files to the new server and retaining its permissions while files and folders are being copied b.Export the shares registry (old server) c.Import the shares registry (new server)
The link below from Microsoft website shows how xcopy can copy folder and retain the permissions.
https://support.microsoft.com/…

Move folders in command line with specific string

How to move folders in command line without selecting manually the folders?
Manually moving folders is quite ideal if there are two or three folders that needs to be moved. But if the folders are quite a lot and doing it manually is a bit tedious; besides chances are there might be folders that will be missed out.
Command lines and some basic editing in notepad can ease the process of moving folders it can be run via batch file or just simply enter the command at the command prompt window.
Assume that all folders have the word “old-data” as part of its folder name. It can be any string as long as it’s common to all the folders that needs to be moved or archived.
First step, is to open a command prompt window then change directory to the folders.
Example: c:\cd sales_data
Then at sales_data directory type:
dir *old-data* /s/b > file_old_data.txt
replace old-data with any string that is common to all folders.
* is used between the string, which basically means that look for folders with this …