This is driving me crazy! I have the below code that works somewhat. What I want it to do is at the 2 points where it has the If Err.Number <> 0 Then statements to execute the code below then loop back to the top to start over again. What it is doing is executing the first computer in the list, then when it finds a machine with an error it displays as if all the following machines have the error; I know these machines work because I have moved them around in the ini file to make sure... Please help if you can, I am kind of new to all this.
Thanks!
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objOutput = objFSO.OpenTextFile(".\services.csv", ForAppending, True)
objOutput.Write ("System Name,IP Address,Date Ran,Caption,Service Status,Startup Type,Service Name,Account Name")
objOutput.Writeline
On Error Resume Next
Set InputFile = objFSO.OpenTextFile(".\computers.ini")
Do While Not (InputFile.atEndOfStream)
strComputer = InputFile.ReadLine
strCommand = "%comspec% /c ping -n 1 -w 100 " & strComputer & ""
Set objExecObject = objShell.Exec(strCommand)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then
' Set objWMIService = GetObject("winmgmts:" _
' & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
If Err.Number <> 0 Then
objOutput.Write(strComputer) & ","
objOutput.Write("RPC Error")
objOutput.WriteLine
Exit
Else
Set objWMIService = objSWbemLocator.ConnectServer _
(strComputer, "root\cimv2", "%computername%\administrator", "Password")
objWMIService.Security_.ImpersonationLevel = 3
If Err.Number <> 0 Then
objOutput.Write(StrComputer) & ","
objOutput.Write("Authentication Error")
objOutput.WriteLine
Exit
Else
Set oServices = objWMIService.ExecQuery ("Select * from Win32_Service")
For Each objService in oServices
objOutput.Write(objService.SystemName) & ","
objOutput.Write(StrComputer) & ","
objOutput.Write(Date) & " "
objOutput.Write(Time) & ","
objOutput.Write(objService.Caption) & ","
objOutput.Write(objService.State) & ","
objOutput.Write(objService.StartMode) & ","
objOutput.Write(objService.Name) & ","
objOutput.Write(objService.StartName) & ","
objOutput.WriteLine
Next
End If
End If
End If
Loop
Loop
objOutput.Close
Thanks!
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objOutput = objFSO.OpenTextFile(".\services.csv", ForAppending, True)
objOutput.Write ("System Name,IP Address,Date Ran,Caption,Service Status,Startup Type,Service Name,Account Name")
objOutput.Writeline
On Error Resume Next
Set InputFile = objFSO.OpenTextFile(".\computers.ini")
Do While Not (InputFile.atEndOfStream)
strComputer = InputFile.ReadLine
strCommand = "%comspec% /c ping -n 1 -w 100 " & strComputer & ""
Set objExecObject = objShell.Exec(strCommand)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then
' Set objWMIService = GetObject("winmgmts:" _
' & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
If Err.Number <> 0 Then
objOutput.Write(strComputer) & ","
objOutput.Write("RPC Error")
objOutput.WriteLine
Exit
Else
Set objWMIService = objSWbemLocator.ConnectServer _
(strComputer, "root\cimv2", "%computername%\administrator", "Password")
objWMIService.Security_.ImpersonationLevel = 3
If Err.Number <> 0 Then
objOutput.Write(StrComputer) & ","
objOutput.Write("Authentication Error")
objOutput.WriteLine
Exit
Else
Set oServices = objWMIService.ExecQuery ("Select * from Win32_Service")
For Each objService in oServices
objOutput.Write(objService.SystemName) & ","
objOutput.Write(StrComputer) & ","
objOutput.Write(Date) & " "
objOutput.Write(Time) & ","
objOutput.Write(objService.Caption) & ","
objOutput.Write(objService.State) & ","
objOutput.Write(objService.StartMode) & ","
objOutput.Write(objService.Name) & ","
objOutput.Write(objService.StartName) & ","
objOutput.WriteLine
Next
End If
End If
End If
Loop
Loop
objOutput.Close