on error resume Next
'
const W2k3_Patch = "WindowsServer2003.WindowsXP-KB2562937-x64-ENU.exe"
const W2k8_Patch = "Windows6.1-KB2562937-x64.msu"
const LocalPatch = "c:\temp\"
Dim strcommand
If right(ucase(wscript.FullName),11)="WSCRIPT.EXE" Then
wscript.echo "ERROR: You must run this script using cscript, for example 'cscript " & wscript.scriptname & "'."
wscript.quit 0
end If
' ipFile = wscript.arguments(0)
ipFile = "C:\Temp\comps.txt"
set ofs = createobject("scripting.filesystemobject")
' Verify that ipfile is accessible.
set oipFile = ofs.opentextfile(ipFile, 1, false)
if (Err.Number <> 0) Then
wscript.echo "Cannot open " & ipFile
wscript.quit
end If
' Make sure to end with a \ character.
if right(LocalPatch, 1) <> "\" Then
LocalPatch = LocalPatch & "\"
end If
'Note that cim_datafile does not support UNC paths 'so everything must be handled through mapped drives.
if left(LocalPatch, 2) = "\\" Then
wscript.echo "<pathToExecutable> cannot be a UNC path, please map a drive locally"
wscript.quit
end If
exeW2k8 = ofs.getfile(LocalPatch + W2k8_Patch).name
exeW2k3 = ofs.getfile(LocalPatch + W2k3_Patch).name
' Verify that the patches are accessible.
if ((len(exeW2k8) = 0) OR (len(exeW2k3) = 0)) Then
wscript.echo "Cannot find patch files."
wscript.echo "Please verify that the <LocalPatch> folder contains all of these files:" & vbCrLf & " " & W2k8_Patch & vbCrLf & " " & W2k3_Patch
wscript.quit
end If
set osvcLocal = getobject("winmgmts:root\cimv2")
'The error-handling code is below the function that may throw one - execute it.
' on error resume next
while not oipFile.atEndOfStream
ip = oipFile.ReadLine()
wscript.echo vbCrLf & "Connecting to " & ip & "..."
Err.Clear
set osvcRemote = GetObject("winmgmts:\\" & ip & "\root\cimv2")
if (Err.Number <> 0) Then
wscript.echo "Failed to connect to " & ip & "."
Else
exeCorrectPatch = detectOSPatch(osvcRemote)
If (exeCorrectPatch <> "") Then
WScript.Echo "Copying installation file"
Ret = ofs.CopyFile(LocalPatch & exeCorrectPatch, "\\" & ip &"\C$\Temp\", True)
if (ret <> 0 and ret <> 10) Then
' Failure detected and failure was not "file already exists."
wscript.echo "Failed copy to " & ip & " - error: " & ret
WScript.Sleep 5000
Else
strComputer = ip
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Create The patch installation process
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create(strCommand, "c:\temp", Null, intProcessID)
If intReturn <> 0 Then
Wscript.Echo "Process could not be created." & vbNewLine & "Command line: " & strCommand & vbNewLine & "Return value: " & intReturn
wscript.quit
Else
Wscript.Echo "Process created." & vbNewLine & "Command line: " & strCommand & vbNewLine & "Process ID: " & intProcessID
Do
WScript.Sleep 5000
blnCheckProcess = CheckProcess(intProcessID)
Loop While blnCheckProcess = False
End If
wscript.echo "Installation successful."
WScript.Echo
WScript.Echo "Deleting file"
Ret = ofs.DeleteFile("\\" & ip & "\C$\Temp\" & exeCorrectPatch, True)
end if 'Create process succeeded.
end if 'Copy succeeded.
'onet.removenetworkdrive "z:", true
end if ' The script knows which patch to install.
'end if
' Do the next IP address, then the next IP address...
Wend
oipFile.close()
WScript.Echo vbCrLf & "Done!" & vbCrLf
Function detectOSPatch(osvcRemote)
set oOSInfo = osvcRemote.InstancesOf("Win32_OperatingSystem")
'Only one instance is ever returned (the currently active OS), even though the following is a foreach.
for each objOperatingSystem in oOSInfo
WScript.Echo objoperatingsystem.version
if (objOperatingSystem.OSType <> 18) Then
' Make sure that this computer is Windows NT-based.
wscript.echo ip & " is not a Windows XP, Windows 2000, or Windows 2003 Server computer."
Else
if (objOperatingSystem.Version = "5.2.3790") Then
' Windows Server 2003.
if (objOperatingSystem.ServicePackMajorVersion = 0) or (objOperatingSystem.ServicePackMajorVersion = 2) Or (objOperatingSystem.ServicePackMajorVersion = 3) then
strCommand = "c:\temp\" & exeW2k3 & " /quiet /norestart"
systemType = exeW2k3
end If
ElseIf (objOperatingSystem.Version = "6.1.7600") or (objOperatingSystem.Version = "6.1.7601") Then
' Windows 2008R2.
if (objOperatingSystem.ServicePackMajorVersion = 0) or (objOperatingSystem.ServicePackMajorVersion = 1) then
strcommand = "WUSA c:\temp\" & exeW2k8 & " /quiet /norestart"
systemType = exeW2k8
end If
end If
if (systemType = "") Then
'This was a Windows NT-based computer, but not with a valid service pack.
wscript.echo "Could not patch " & ip & " - unhandled OS version: " & objOperatingSystem.Caption & " SP" & objOperatingSystem.ServicePackMajorVersion & "("& objOperatingSystem.Version & ")"
end If
end If
Next
detectOSPatch = systemType
end Function
Function CheckProcess(P_intProcessID)
CheckProcess = False
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE ProcessID = '" & P_intProcessID & "' ")
If colProcesses.Count = 0 Then
WScript.Echo "No proccesses with ID " & P_intProcessID & " are running"
CheckProcess = True
Else
For Each objProcess in colProcesses
WScript.Echo objProcess.ProcessID & ": " & objProcess.CommandLine
Next
End If
End Function 'CheckProcess
' Win32_Process.Create Return Codes
' Value Description
' 0 Successful Completion'
'
' 2 Access Denied
'
' 3 Insufficient Privilege
'
' 8 Unknown failure
'
' 9 Path Not Found
'
' 21 Invalid Parameter