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).ShortPath
rbcopy = 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, True

Loop

25 thoughts on “Installing Patches/Application remotely on windows Machine using Psexec

  1. Its not working for me. It do copy the files in the specified directory on remote machine but nothing else. I cant see patch applied to the machine in window update history.
    I really need this solution as i have to deploy many patches manually as WSUS is not doing the work.
    Please help

  2. Hi Krishna!

    Thanks for the reply. Yes the hotfix are in C:\Hotfixes both on source and destination computers.

    Its works fine with copying but it cant execute the file.

  3. I entered file name direct:
    strCmd = “cmd /C ” & strPSExec & ” \\” & strComputer & ” “”C:\Hotfixes\SQLServer2008R2-KB2494088-x64.exe”” /quite”
    but even that didnt help. I can see in a flash screen that its says
    The system cannot find the specified file.

  4. ok finally i manage to run it with manual way i.e inserting the file name instead of . The script copies the file and run for more than 1 min and i can see some message in new command prompt in a flash and thats it. I cant find the KB installed on the remote computer.
    Any way i can print the error message on the screen or file?

  5. You must edit the line in the script: “”C:\Hotfixes\””
    Change into your patch file name eg. “”C:\Hotfixes\KB999999.exe””

    Also, there’s a typo in the script. /quite must be fixed to /quiet

  6. how would I install a list of patches from a folder that contains more than 20 patches to machines that few will need 5 patches other 8 and the rest 20 patches…. any input on this one… this is my dilemma that I have…. please help

  7. Hi Krishna,

    i would want to install the patch but not to reboot the server, any additional lines should be added to the script?

    Thank You

    • Hi Madhu,

      Refer below link & u can generate report for multiple computers / server on one KB# / Hotfix #

      https://gallery.technet.microsoft.com/scriptcenter/Generate-a-Report-for-f71a6800

      copy & save below code in notepad & rename to anyname.ps1 grant full permission to yourself & try your luck

      computers.txt – mention the name of the servers

      $computers = cat c:\computers.txt
      $Patch = “KB2819745”
      foreach ($computer in $computers)
      {
      if (get-hotfix -id $Patch -ComputerName $computer -ErrorAction 2)
      {
      Add-content “Hotfix is Present in $computer” -path “$env:USERPROFILE\Desktop\Hotfix-Present.log”
      }
      Else
      {
      Add-content “Hotfix is not Present in $computer” -path “$env:USERPROFILE\Desktop\Missing-Hotfix.log”
      }
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s