Guest Jeferson Propheta Posted August 7, 2007 Posted August 7, 2007 *********************************************************************************************************** '* Wsus Remote Force in Network Range * '* ------------------------------------- * '* Written by : Jeferson Propheta * '* Date : August / 2007 * '* Description: Create 2 Process in a Remoter Computer using a stipulate Range in order to Force WSUS * '* Detection and Report * '* ------------------------------------- * '* Configure #Sessions * '* #1 - to specify the Location of Log File and Log File Name * '* #2 - to mount the Network Range * '* * '* * '*************************************************************************************************************** On Error Resume Next Const HKEY_LOCAL_MACHINE = &H80000002 Const ForReading = 1, ForAppending = 8 '*************************************************************************************************************** '* WSUS Commands * '* ------------------------------------- * '*************************************************************************************************************** strWsusCommand = "wuauclt /ResetAuthorization /DetectNow" strWsusCommand2 = "wuauclt /ReportNow" ServiceName = "wuauserv" '*************************************************************************************************************** '* Script initial Sets * '* ------------------------------------- * '*************************************************************************************************************** Set objShell = CreateObject("WScript.Shell") Set objOUTFSO = CreateObject("Scripting.FileSystemObject") '#Session 1 '*************************************************************************************************************** '* Output File name and Folder * '* ------------------------------------- * '*************************************************************************************************************** strOutDirectory = "C:\Temp\" strOutFile = "\WsusRangeForceReport.txt" LineSeparator = 48 LineSeparatorChar = "-" '*************************************************************************************************************** '* Output Folder Structure and File Creation * '* ------------------------------------- * '*************************************************************************************************************** If objOUTFSO.FolderExists(strOutDirectory) Then Set objFolder = objOUTFSO.GetFolder(strOutDirectory) Else Set objFolder = objOUTFSO.CreateFolder(strOutDirectory) End If If objOUTFSO.FileExists(strOutDirectory & strOutFile) Then Set objFolder = objOUTFSO.GetFolder(strOutDirectory) strOutpath = strOutDirectory & strOutFile objOUTFSO.DeleteFile(strOutpath) Else Set objFile = objOUTFSO.CreateTextFile(strOutDirectory & strOutFile) End If '#Session 1 '*************************************************************************************************************** '* Subnet Settings * '* ------------------------------------- * '*************************************************************************************************************** Subnet = "192.168.25" 'Subnet Start For ii = 1 to 7 'i.e.: 192.168.251.xxx to 192.168.257.xxx For i = 0 to 254 'i.e.: 192.168.251.0 to 192.168.257.254 strTarget = Subnet & ii & "." & i '*************************************************************************************************************** '* Target IP Test * '* ------------------------------------- * '*************************************************************************************************************** Set objWMIServicePing = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIServicePing.ExecQuery ("Select * from Win32_PingStatus " & "Where Address = '" & strTarget & "'") Set objFile = Nothing Set objFolder = Nothing Set objOutTextFile = objOUTFSO.OpenTextFile (strOutDirectory & strOutFile, ForAppending, True) For Each objItem in colItems If objItem.StatusCode = 0 Then strComputer = strTarget '*************************************************************************************************************** '* Check Service * '* ------------------------------------- * '*************************************************************************************************************** If ServiceExist(ServiceName) = True Then Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colServices = objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Name = 'wuauserv'") For Each objService in colServices errReturnCode = objService.StopService() errReturnCode = objService.StartService() Next '*************************************************************************************************************** '* Write IP Address and Response Time * '* ------------------------------------- * '*************************************************************************************************************** Set objProcess = objWMIService.Get("Win32_Process") objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) objOutTextFile.WriteLine ("IP Address : " & strTarget) objOutTextFile.WriteLine ("Reply received at : " & Now()) '*************************************************************************************************************** '* Start Process One * '* ------------------------------------- * '*************************************************************************************************************** intReturn = objProcess.Create (strWsusCommand, Null, Null, intProcessID) If intReturn = 0 Then objOutTextFile.WriteLine ("Process ID 1 Created : " & intProcessID) Else objOutTextFile.WriteLine ("Process 1 could not be created!!!!") End If '*************************************************************************************************************** '* Start Process Two * '* ------------------------------------- * '*************************************************************************************************************** intReturn = objProcess.Create (strWsusCommand2, Null, Null, intProcessID) If intReturn = 0 Then objOutTextFile.WriteLine ("Process ID 2 Created : " & intProcessID) Else objOutTextFile.WriteLine ("Process 2 could not be created!!!!") End If Else '*************************************************************************************************************** '* Write Cannot Find the Service (Probably 9x or Non-Windows O.S.) * '* ------------------------------------- * '*************************************************************************************************************** objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) objOutTextFile.WriteLine ("IP Address : " & strTarget & " Cannot Find the Service... Process Aborted!") End IF Else '*************************************************************************************************************** '* Write Ping Not-Reply * '* ------------------------------------- * '*************************************************************************************************************** objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) objOutTextFile.WriteLine ("IP Address : " & strTarget & " Reply Not received " & Now()) End If Next Next Next '*************************************************************************************************************** '* Write End of Log * '* ------------------------------------- * '*************************************************************************************************************** objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) objOutTextFile.WriteLine ("WSUS Range Force v0.1") objOutTextFile.WriteLine ("This Report summarizes the status of the Update Services Force in the Range.") objOutTextFile.WriteLine ("Report Finished at: " & Now()) objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) objOutTextFile.WriteLine String(LineSeparator, LineSeparatorChar) Wscript.Quit '*************************************************************************************************************** '* Services Function * '* ------------------------------------- * '*************************************************************************************************************** Function ServiceExist(ServiceName) strComputer = strTarget Set objWMIService = GetObject("winmgmts:\\" & strTarget & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service",,48) For Each objItem in colItems If objItem.Name = ServiceName Then ServiceExist = True End If Next End Function Function SetStartType(StartMode, ServiceName) If ServiceExist(ServiceName) = True Then Set objWMIService = GetObject("winmgmts:\\" & strTarget & "\root\CIMV2") Set objShare = objWMIService.Get("Win32_Service.Name='" & ServiceName & "'") Set objInParam = objShare.Methods_("ChangeStartMode").inParameters.SpawnInstance_() objInParam.Properties_.Item("StartMode") = StartMode Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='"& ServiceName & "'", "ChangeStartMode", objInParam) SetStartType = objOutParams.ReturnValue Else SetStartType = 9 End If End Function
Recommended Posts