Archive for September 2013

I am back here after long time  , and this time again I am up with one more interesting PowerShell script.

Script description :

  This Script will trigger installation of Microsoft WSUS patch updates on remote system. Script has multiple functions , each takes care of different tasks.

  The servers which needs to be patched remotely should be placed in servers.txt . Script will read the servers.txt and uses psexec.exe to initiate the patch installation.

 Update.vbs is a part of the script , and it has to be pushed to the remote computer where scripts triggers the installation of patches. PowerShell utlises this Update.vbs and installs the updates. Once updates are installed , this updates.vbs will be removed from the remote system.

Additionally , Script will prompt for restart option of remote system , ‘Y’ or ‘y’ will trigger the restart on remote machine , also it checks for server online (RDP Status) post reboot.

Below is the script 🙂


Set objSession = CreateObject(“Microsoft.Update.Session”)
Set AutoUpdate = CreateObject(“Microsoft.Update.AutoUpdate”)

WScript.Echo “Searching for updates…”

Set UpdateSearcher = objSession.CreateUpdateSearcher
Set SearchResult = UpdateSearcher.Search(” IsAssigned=1 and IsHidden=0 and IsInstalled=0 and Type=’Software'”)


If searchResult.Updates.Count = 0 Then
End If

Set updatesToDownload = CreateObject(“Microsoft.Update.UpdateColl”)
For i = 0 To SearchResult.Updates.Count-1
    Set update = SearchResult.Updates.Item(I)
    If Not update.EulaAccepted Then update.AcceptEula

WScript.Echo “Downloading the Updates”

Set downloader = objSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload

Set UpdatesToInstall = CreateObject(“Microsoft.Update.UpdateColl”)
For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
    End If
WScript.Echo “Installating the Updates…..”
Set installer = objSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall


PowerShell Script

# Name : Install-Patches on remote system
#Author : Prashant Girennavar.
#DateCreated : 4th Sept 2013.

# Function to create a Temp folder on remote machine if it does not exist
Function TempFolderCheck($Server)
 If(!(Test-Path \\$Server\c$\temp))
   New-Item -type directory -path \\$Server\c$\Temp

#Function to copy Update.vbs script on remote machine
Function BatchVBCopy($Server)
Write-host “VB Script is being copied on $Server”
Copy-Item -Path \\$SourceServer\C$\update.vbs -destination \\$Server\c$\Temp

#Function to trigger the patch installation on remote machine
Function InstallPatch($Server)
 .\psexec.exe -accepteula -s -i \\$Server cscript.exe C:\temp\update.vbs
 if($LASTEXITCODE -eq 0) #Check if PSEXEC executed successfully
 Write-Host “$Server” Patched Successfully
 Remove-Item \\$Server\C$\Temp\Update.vbs #Remove the update.vbs from remote system
 $Reboot =  Read-Host “Do you wish to Reboot the system now? say Y to reboot / N to decline”
 if(($Reboot -eq “Y”)-or($Reboot -eq “y”))
  Write-Host “Now Rebooting the $Server , Please wait ….”
  Restart-Computer -ComputerName $Server -Force #Restarting remote computer post patch installation
 Write-Host “$Server” Unable to patch the server please check
Write-Host “$Server” Encountered an exception

$Servers = Get-Content C:\Servers.txt # Get the server list which needs to be patched
Foreach($Server in $Servers)
TempFolderCheck($Server) #Call TempFolderCheck Function
BatchVBCopy($Server) #Call BatchVBCopy Function
InstallPatch($Server) #Call InstallPatch Function

Start-Sleep -Seconds 300 #Sleep for 5 mins

#Checking for server online status
Write-Host “Now Checking if all the servers are online post patch installation”
$Servers = Get-Content C:\Servers.txt
Foreach($Server in $Servers)
 $Connection = New-Object Net.Sockets.TcpClient # Add RDP Port .NET class
 $Connection.Connect($Server,’3389′) #Check for RDP Port 3389 status
 if ($Connection.Connected) #Check connection has been established
  Write-Host “$Server is online after patching” -ForegroundColor Green
 Write-Host ” $Server seems to have some problem , Please check it manually” -ForegroundColor Red

$SourceServer = > where Update.VBS is stored

I Hope this script will help