Wednesday, April 1, 2009

Update windows clients manually from WSUS

This script when it is run on any client enables you to update manually the client with the updates available from WSUS server. The client PC should be configured aso update from the WSUS. The script is given below:

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
Set File = CreateObject("Scripting.FileSystemObject")
logOutput = 1

If logOutput > 1 Then
Set LogFile = File.OpenTextFile("c:\WUSforceupdate.log", 8, True)
LogFile.WriteLine("***************************************************************")
LogFile.WriteLine( "START TIME : " & now)
LogFile.WriteLine( "Searching for updates..." & vbCRLF)
LogFile.WriteLine( "List of applicable items on the machine:")
End If

If logOutput > 0 Then
WScript.echo("***************************************************************")
WScript.echo( "START TIME : " & now)
WScript.echo( "Searching for updates..." & vbCRLF)
WScript.echo( "List of applicable items on the machine:")
End If

For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)

If logOutput > 1 Then LogFile.WriteLine( I + 1 & "> " & update.Title)
If logOutput > 0 Then wscript.echo ( I + 1 & "> " & update.Title)

Next

If searchResult.Updates.Count = 0 Then
If logOutput > 1 Then LogFile.WriteLine( "There are no applicable updates.")
If logOutput > 0 Then WScript.echo( "There are no applicable updates.")

WScript.Quit
End If

If logOutput > 1 Then LogFile.WriteLine( VbCrLf & "Creating collection of updates to download:")
If logOutput > 0 Then WScript.echo( VbCrLf & "Creating collection of updates to download:")

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
If logOutput > 1 Then LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
If logOutput > 0 Then WScript.Echo ( I + 1 & "> adding: " & update.Title )
updatesToDownload.Add(update)
Next

If logOutput > 1 Then LogFile.WriteLine( vbCRLF & "Downloading updates...")
If logOutput > 0 Then WScript.Echo ( vbCRLF & "Downloading updates...")

Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()

If logOutput > 1 Then LogFile.WriteLine( vbCRLF & "List of downloaded updates:")
If logOutput > 0 Then WScript.Echo ( vbCRLF & "List of downloaded updates:")

For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
If update.IsDownloaded Then

If logOutput > 1 Then LogFile.WriteLine( I + 1 & "> " & update.Title )
If logOutput > 0 Then WScript.Echo ( I + 1 & "> " & update.Title )
End If
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")


If logOutput > 1 Then LogFile.WriteLine( vbCRLF & "Creating collection of downloaded updates to install:" )
If logOutput > 0 Then WScript.Echo ( vbCRLF & "Creating collection of downloaded updates to install:" )

For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
If logOutput > 1 Then LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
If logOutput > 0 Then WScript.Echo ( I + 1 & "> adding: " & update.Title )
updatesToInstall.Add(update)
End If
Next


If logOutput > 1 Then logFile.WriteLine( "Installing updates...")
If logOutput > 0 Then WScript.Echo ( "Installing updates...")

Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
If logOutput > 0 Then
'Output results of install to screen
WScript.echo( "Installation Result: " & installationResult.ResultCode )
WScript.echo( "Reboot Required: " & installationResult.RebootRequired & VbCrLf )
WScript.echo( "Listing of updates installed " & "and individual installation results:" )
End If

If logOutput > 1 Then
'Output results of install to file
LogFile.WriteLine( "Installation Result: " & installationResult.ResultCode )
LogFile.WriteLine( "Reboot Required: " & installationResult.RebootRequired & VbCrLf )
LogFile.WriteLine( "Listing of updates installed " & "and individual installation results:" )
End if

For I = 0 To updatesToInstall.Count - 1
If logOutput > 1 Then LogFile.WriteLine( I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode )
If logOutput > 0 Then WScript.Echo ( I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode )
Next

'//Need to reboot?
if installationResult.RebootRequired = -1 Then
If logOutput > 1 Then LogFile.WriteLine( "reboot the machine")
If logOutput > 0 Then WScript.Echo ( "reboot the machine")
end If

If logOutput > 1 Then
LogFile.WriteLine( "STOP TIME : " & now)
LogFile.WriteLine("***************************************************************")
LogFile.Close
End If

If logOutput > 0 Then
WScript.Echo ( "STOP TIME : " & now)
WScript.Echo ("***************************************************************")
End if