'Identificar maquinas Windows 2000 SEM O PATCH MS-05-039! 'Option Explicit On Error Resume Next const ForReading = 1 Dim ArrText() Dim iUpperBound:iUpperBound = 0 Dim oFSO,OTF,Machine,DN,MachineObject Dim bPing:bPing=false Dim bPatched:bPatched=false Dim bCopied:bCopied=false Set oFSO = CreateObject("scripting.filesystemobject") 'Arquivo que contem o DN das máquinas windows 2000 com sp > 0 separados por "\n" Set oTF = oFSO.OpenTextFile("C:\producao\machines.csv",ForReading,True) 'Vamos ler o arquivo, randomizar a leitura do array que contem os hosts para 'evitarmos de ficar buscando os mesmos hosts em maquinas distintas. Talvez o ideal seja 'criar um MUTEX para garantir que o programa roda sozinho. While Not otf.AtEndOfStream ReDim Preserve arrText(iUpperBound) arrText(UBound(arrText)) = ts.ReadLine iUpperBound = iUpperBound + 1 Wend Randomize count = Int((max-min+1)*Rnd+min) Dim count:count = 0 dim max,min max=(iUpperBound - 1) min=0 while count < iUpperBound DN = arrText(count) count = count + 1 set MachineObject = GetObject("LDAP://" & DN) machine = MachineObject.cn bPing = PingHost(machine) if(bPing = true) then bPatched = ListaPatches(machine) if(bPatched = true) then wscript.Echo "[OK] " & machine else wscript.Echo "[FUCKED] " & machine bCopied = CopiaArquivos(machine) if(bCopied = true) then RunRemote(machine) Else wscript.Echo "falha na copia " & machine End if end if else wscript.Echo machine & "is unreachable!!" end if Wend 'Lista Os patches da maquina function ListaPatches(computer) Dim owMIService,colServices,objService,Query,count Dim i:i=0 'wscript.Echo "Conectando no computador " & Computer Set oWMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2") Query = "SELECT * FROM Win32_QuickFixEngineering WHERE HotfixID='KB899588'" Set colServices = oWMIService.ExecQuery(Query,,48) for each objService in colServices i = i + 1 'wscript.Echo objService.HotfixID next if(i > 0) then 'PATCHIADO ListaPatches = True Else 'NAO PATCHIADO ListaPatches = False end if End function 'Como rodo como Domain Admin, vou jogar no C$ Function CopiaArquivos(computer) Dim strRemoteFolder,ArrFiles,strFile,intCount intCount = 0 strRemoteFolder = "\\" & computer & "\c$\patch" 'wscript.Echo "Remote Dir = " & strRemoteFolder Set objFSO = CreateObject("Scripting.FileSystemObject") If Not objFSO.FolderExists(strRemoteFolder) Then Err.Clear objFSO.CreateFolder(strRemoteFolder) If Err = 0 Then WScript.Echo "Pasta Criada " & strRemoteFolder & "." Else WScript.Echo "Unable to create folder " & strRemoteFolder & "." WScript.Echo "Error Number: " & Err.Number WScript.Echo "Error Source: " & Err.Source WScript.Echo "Error Description: " & Err.Description Err.Clear CopiaArquivos = False Exit Function End If End if 'Vamos copiar os arquivos arrFiles = Array("aplica.bat","Windows2000-KB899588-x86-ENU.EXE","machine_patch.vbs","machines.csv") For Each strFile in arrFiles If objFSO.FileExists(strFile) Then objFSO.CopyFile strFile, strRemoteFolder & "\" If Err = 0 Then WScript.Echo "Copiado " & strFile & " -> " & strRemoteFolder intCount = intCount + 1 Else WScript.Echo "Nao copiado " & strFile End If Err.Clear Else WScript.Echo "File " & strFile & " not found." End If Next If (intCount = UBound(arrFiles) + 1) Then CopiaArquivos = True Else CopiaArquivos = False End If End Function Function PingHost(computer) Dim objShell,objExec,strPingResults Set objShell = CreateObject("WScript.Shell") Set objExec = objShell.Exec("ping -n 2 -w 1000 " & Computer) strPingResults = LCase(objExec.StdOut.ReadAll) If InStr(strPingResults, "reply from") Then PingHost = True Else PingHost = False End If End Function 'Roda o Batch que starta o patching e roda novamente o machine_patch.vbs contra as maquinas do machine.csv Sub RunRemote(computer) strScript = "C:\patch\aplica.bat" 'Connect to WMI service on remote machine. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2") If Err <> 0 Then WScript.Echo "Error connecting to WMI on \\" & Computer & "." WScript.Echo "Error number: " & Err.Number WScript.Echo "Error source: " & Err.Source WScript.Echo "Error description: " & Err.Description Error.Clear Exit Sub End If 'Get a Win32_Process object. Set objProcess = objWMIService.Get("Win32_Process") 'Create a new process and run strScript in it. intReturn = objProcess.Create(strScript, , , intProcessID) If intReturn = 0 Then WScript.Echo "Launched " & strScript & VbCrLf & "Process ID: " & intProcessID & "." Else WScript.Echo "Error: " & strScript & " could not be started." & VbCrLf & "Return code: " & intReturn & "." End If End Sub