Installing Patches/Application remotely on windows Machine using Psexec

Installing pathes/Application on a remote computer is not a tough one when you have lots of Microsoft and third party application to do this for us.Chances that few companies still does not wanted to relay on software to install patches and Administrator manually installed this. You should ask this question to the Administrators who works on weekend installing patches manually, do you like this to do every weekend ? I am sure he will give you very dirty look for sure 🙂

Below is the small piece of code you may like to use it for installing pathces or any small application on various computer remotely.

Prerequisits
1. Download PSExec.exe from Microsoft.com (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) and copy to C:\Psexec.exe
2. Create C:\Hotfixes and dump the path here
3. Copy below code and save it as C:\patchinstall.vbs
4. Create C:\Servers.txt with list of servername
5. Rename with the appropriate name
6. Open command prompt type cscript C:\patchinstall.vbs

This code will copy the patch to all the remote computer mentioned in C:\Servers.txt under C:\Hotfixes and it uses psexec.exe to install the patch on the remote computer

Set objShell = CreateObject("Wscript.Shell")Set objFSO = CreateObject("Scripting.FileSystemObject") set filetxt = objFSO.OpenTextFile("C:\Servers.txt",1)strPSExec = "C:\PSExec.exe"rbcopy = "c:\Windows\System32\Robocopy.exe"strPSExec = objFSO.GetFile(strPSExec).ShortPathrbcopy = objFSO.GetFile(rbcopy).ShortPath do Until filetxt.AtEndOfStream strComputer = filetxt.Readline strComputer = trim(strComputer)   strCmd = "cmd /C " & rbcopy  & " C:\Hotfixes" & " \\" & strComputer & "\c$\Hotfixes" wscript.echo strcmd objShell.Run strCmd, 1, True  strCmd = "cmd /C " & strPSExec & " \\" & strComputer & " ""C:\Hotfixes\<Hotfixname.exe>"" /quite" wscript.echo strcmd objShell.Run strCmd, 1, TrueLoop

TaskKill.exe to Kill the process on the remote computer

Taskkill.exe is very great tool which come in handy when you wanted to kill or terminate a process on the remote computer or local computer. You would do easily in the local computer and if you wanted to so the same in remote computer then you would wanted some easy option. you dont have to know the exact process Id or the Process name. Even you can use Wild card to find the process and kill it.

Below is the example to kill process running on the remote computer and you also force to terminate it.

taskkill /s <servername> /f /im Processname*

Below link has detail instruciton on how to use Taskkill.exe with various options

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/taskkill.mspx

Powershell to work on Remote Registery

Working on Remote Registry is always challenging. If you are administrator, I am sure you would have come across various times that you wanted to update registry of the computer and you have to login to the each server and apply the registry. Powershell has made most of our administrator’s life easier. Let’s understand how we can access and edit registry of remote computer using powershell

Let’s understand code with example. Below Script take the list of server name in CSV file and reads each server name one by one and opens the registry Key and prints the value. In this fashion you can get any key and get the value. In the below script I am trying to get the value of HKEY_CurrentUsers. This is the reason current in OpenRemoteBaseKey. If you Access keys of HKEY_Localmachine and use LocalMachine

$csv = Import-Csv "C:\Server.csv"foreach($line in $csv){ $strComputer = $line.CMSName $curreg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser', $strComputer) $curkey="Software\Microsoft\Windows\CurrentVersion\Wintrust\Trust Providers\Software Publishing" $curregKey = $curreg.OpenSubKey($curkey) $curregkey.GetValue('State')} 

If you wanted to get the list of subfolder under the folder in the registry then use the below code. This will get list of all the subfolder MSExchangeIS\Clustername. Getsubkeynames() does this.

<strong>$csv = Import-Csv "C:\Server.csv"foreach($line in $csv) { $cmsName = $line.Node $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strComputer) $key="SYSTEM\CurrentControlSet\Services\MSExchangeIS\$cmsName" $regKey = $reg.OpenSubKey($key) foreach($Rkey in $regKey.getsubkeynames())  {  $Rkey   } }

If you wanted to write or edit the remote registry then below code will help. This code will create the new key if the key does not exist else it will edit the existing key to the required value. Below code will create the Dword key if key does not exit there else it will edit the existing key to the required value on the entire computer mentioned in the CSV file.

$csv = Import-Csv "C:\Server.csv"foreach($line in $csv){ $strComputer = $line.Node $curreg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser', $strComputer) $curkey="Software\Microsoft\Windows\CurrentVersion\Wintrust\Trust Providers\Software Publishing" $curregKey = $curreg.OpenSubKey($curkey,$True) $curregkey.SetValue('State','100234','Dword')}