|
wbracken -> RE: On-demand patching in SCCM (9/12/2008 12:34:44 PM)
|
I think you may mean something like this? The attached code will download if needed and install any approved patches. Since SCCM uses WSUS it should work the same. this runs silently and forces a reboot if needed but you could modify as needed.
on error resume next
'######################################################################
'# Initialize
'######################################################################
Const FORCEDRESTART = 6
Const HKEY_LOCAL_MACHINE = &H80000002
Dim updateSession, updateSearcher, updatesToDownload, updatesToInstall
Dim objReg, strKeyPath, strValueName, regWSUSServer
Dim updateServerIsOnline
Dim objHttpRequest
Dim searchResult
Dim update
Dim downloader
Dim I
Dim installer
Dim installationResult
Dim computerName
Dim objShell, objWMIService, colOperatingSystems, objOperatingSystem,objnetwork
Dim strEventText
Dim EmailAddresses
Dim EmailMessage
computerName = "."
strEventText = ""
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
Set objShell = Wscript.CreateObject("Wscript.Shell")
set objnetwork=wscript.createobject("wscript.network")
'######################################################################
'#Begin Installation Process
'######################################################################
objShell.LogEvent 4, "WUSUS has been initiated."
'######################################################################
'# Verify the availability of our Updates server (i.e. WSUS or Windows Update)
'######################################################################
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
computerName & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
strValueName = "WUServer"
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,regWSUSServer
Set objHttpRequest = CreateObject("Msxml2.XMLHTTP")
objHttpRequest.open "HEAD", regWSUSServer, False
objHttpRequest.send
If Err.Number <> 0 Then
updateServerIsOnline = false
objShell.LogEvent 2, "The WSUS Server, " & regWSUSServer &_
" is not available, Terminating forced install."
wscript.quit
Else
updateServerIsOnline = true
End If
'######################################################################
'# Search for available updates and notify the Application Event Log
'######################################################################
updateSearcher.Online = updateServerIsOnline
Set searchResult = _
updateSearcher.Search("IsInstalled=0 and IsAssigned=1")
If searchResult.Updates.Count = 0 Then
' There are no applicable updates
objShell.LogEvent 2, "No updates to install. Script terminated."
WScript.Quit
End If
strEventText = "The following updates will be installed:" & vbCRLF
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
strEventText = strEventText & vbTab & I + 1 & "> " & update.Title & vbCRLF
Next
strEventText = strEventText & vbCRLF & "Proceeding with download and installation...." & vbCRLF
objShell.LogEvent 4, strEventText
'######################################################################
'# Search for updates that still need to be downloaded and download them
'######################################################################
If updateServerIsOnline Then 'Check to make sure we are in online mode first
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
strEventText = "Downloading the following updates:" & vbCRLF
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
If update.IsDownloaded = False Then
strEventText = strEventText & vbCRLF & I + 1 & "> " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count < 1 Then
'Do Nothing
Else
objShell.LogEvent 4, strEventText
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
End If
End If
'######################################################################
'# Create 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
updatesToInstall.Add(update)
End If
Next
'######################################################################
'# Install updates
'######################################################################
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
Next
If installationResult.RebootRequired = true Then
objShell.LogEvent 4, "Completed the installation of updates. Restarting Windows..."
'Open a WMIService connection to the local machine
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
computerName & "\root\cimv2")
'Obtain the local OS Collection
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Use the Win32Shutdown method to force the restart
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(FORCEDRESTART)
Next
Else
objShell.LogEvent 4, "Completed the installation of updates. No restart is required."
End If
|
|
|
|