Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Vbs script that check all machines in a file if a patch is installed. I get a lot of machines with permission error.

Hi,

Vbs script that check all machines in a file if a patch is installed. I get a lot of machines with permission error.
But other scripts work fine in the same conditions. If the machine is Off need to get the results as Switched off.
can anyone help with checking what the issue is.
The script breaks some times on some specific machines. I dont know why. I get an error. Can an condition be added as to skip any kind of error and record all errors.

Regards
Sharath
'Const ADS_SCOPE_SUBTREE = 2
Const ForWriting = 2
 
'Set objConnection = CreateObject("ADODB.Connection")
'Set objCommand = CreateObject("ADODB.Command")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set out = objFSO.CreateTextFile("c:\out.txt", True)
 
inFile = "C:\machines.txt"
arrComputers = Split(objFSO.OpenTextFile(inFile).ReadAll, vbNewLine)
 
'objConnection.Provider = "ADsDSOObject"
'objConnection.Open "Active Directory Provider"
 
'Set objCOmmand.ActiveConnection = objConnection
'objCommand.CommandText = "Select Name, Location from 'LDAP://DC=development,dc=co,dc=uk' " & "WHERE objectClass='computer' " & "and operatingSystemVersion = '5.1 (2600)'"
'objCommand.Properties("Page Size") = 1000
'objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
'Set objRecordSet = objCommand.Execute
'objRecordSet.MoveFirst
 
'Do Until objRecordSet.EOF
For Each strComputer In arrComputers
'        strComputer = objRecordSet.Fields("Name").Value
       qFound = false
        If Reachable(strComputer) Then
                On Error Resume Next
                Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                If Err.Number <> 0 Then
                        out.WriteLine "no permission On " & strComputer & vbCrLf
                Else
                        On Error GoTo 0
                End If
                
                Set colQuickFixes = objWMIService.ExecQuery _
                ("Select * from Win32_QuickFixEngineering")
                 
                If colQuickFixes Is Nothing Then
                    out.WriteLine "Computer: " & strComputer & " NO QuickFix records" & vbCrLf
                Else
                    For Each objQuickFix In colQuickFixes
                            If InStr(objQuickFix.HotFixID, "958644") Then
                                    out.WriteLine "Computer: " & objQuickFix.CSName & " YES" & vbCrLf
                                    qfound = true
                            End If
                    Next
                    if not qfound then out.WriteLine "Computer: " & strComputer & " NO" & vbCrLf
                End If
        Else
                out.WriteLine strComputer & " isnt reachble " & vbCrLf
        End If
        'objRecordSet.MoveNext
'Loop
Next 'For Each strComputer
 
 
Function Reachable(strComputer)
                         
        strCmd = "ping -n 1 " & strComputer
                         
        Set objShell = CreateObject("WScript.Shell")
        Set objExec = objShell.Exec(strCmd)
        strTemp = UCase(objExec.StdOut.ReadAll)
                         
        If InStr(strTemp, "REPLY FROM") Then
                Reachable = True
        Else
                Reachable = False
        End If
End Function

Open in new window

Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

try running as a GPO startup script.
 it then runs with as System user, which may have more permissions. Certainly on XP, not quite so on Vista i'm finding out.
Avatar of bsharath

ASKER

Any way via this script
Try this....I have just re-ordered it a bit.

Regards,

Rob.
Const ForWriting = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set out = objFSO.CreateTextFile("c:\out.txt", True)
 
inFile = "C:\machines.txt"
arrComputers = Split(objFSO.OpenTextFile(inFile).ReadAll, vbNewLine)
 
For Each strComputer In arrComputers
	qFound = False
	If Ping(strComputer) Then
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		If Err.Number <> 0 Then
			Err.Clear
			On Error GoTo 0
			out.WriteLine "no permission On " & strComputer & VbCrLf
		Else
			On Error GoTo 0
			Set colQuickFixes = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering")
			If colQuickFixes Is Nothing Then
				out.WriteLine "Computer: " & strComputer & " NO QuickFix records" & VbCrLf
			Else
				For Each objQuickFix In colQuickFixes
					If InStr(objQuickFix.HotFixID, "958644") Then
						out.WriteLine "Computer: " & objQuickFix.CSName & " YES" & VbCrLf
						qFound = True
					End If
				Next
				If Not qFound Then out.WriteLine "Computer: " & strComputer & " NO" & VbCrLf
			End If
		End If    
	Else
		out.WriteLine "Cannot ping " & strComputer & VbCrLf
	End If
Next 'For Each strComputer
 
Function Ping(strComputer)
    Dim objShell, boolCode
    Set objShell = CreateObject("WScript.Shell")
    boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
    If boolCode = 0 Then
        Ping = True
    Else
        Ping = False
    End If
End Function

Open in new window

Rob i still get this error

---------------------------
Windows Script Host
---------------------------
Script:      C:\Patch search from file.vbs
Line:      24
Char:      5
Error:      Permission denied
Code:      800A0046
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
Hmmm, try this.

Regards,

Rob.
Const ForWriting = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set out = objFSO.CreateTextFile("c:\out.txt", True)
 
inFile = "C:\machines.txt"
arrComputers = Split(objFSO.OpenTextFile(inFile).ReadAll, vbNewLine)
 
For Each strComputer In arrComputers
	qFound = False
	If Ping(strComputer) Then
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		If Err.Number <> 0 Then
			Err.Clear
			On Error GoTo 0
			out.WriteLine "no permission On " & strComputer & VbCrLf
		Else
			On Error GoTo 0
			Set colQuickFixes = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering")
			If colQuickFixes Is Nothing Then
				out.WriteLine "Computer: " & strComputer & " NO QuickFix records" & VbCrLf
			Else
				On Error Resume Next
				For Each objQuickFix In colQuickFixes
					If Err.Number = 0 Then
						On Error GoTo 0
						If InStr(objQuickFix.HotFixID, "958644") Then
							out.WriteLine "Computer: " & objQuickFix.CSName & " YES" & VbCrLf
							qFound = True
						End If
					Else
						Err.Clear
						On Error GoTo 0
						out.WriteLine "Computer: " & strComputer & " Permission denied" & VbCrLf
					End If
				Next
				If Not qFound Then out.WriteLine "Computer: " & strComputer & " NO" & VbCrLf
			End If
		End If    
	Else
		out.WriteLine "Cannot ping " & strComputer & VbCrLf
	End If
Next 'For Each strComputer
 
Function Ping(strComputer)
    Dim objShell, boolCode
    Set objShell = CreateObject("WScript.Shell")
    boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
    If boolCode = 0 Then
        Ping = True
    Else
        Ping = False
    End If
End Function

Open in new window

Rob i get this error
---------------------------
Windows Script Host
---------------------------
Script:      C:\Patch search from file.vbs
Line:      20
Char:      4
Error:      Generic failure
Code:      80041001
Source:       SWbemServicesEx

---------------------------
OK  
---------------------------
Hmmm.  What about this?
Const ForWriting = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set out = objFSO.CreateTextFile("c:\out.txt", True)
 
inFile = "C:\machines.txt"
arrComputers = Split(objFSO.OpenTextFile(inFile).ReadAll, vbNewLine)
 
For Each strComputer In arrComputers
	qFound = False
	If Ping(strComputer) Then
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		If Err.Number <> 0 Then
			Err.Clear
			On Error GoTo 0
			out.WriteLine "no permission On " & strComputer & VbCrLf
		Else
			Err.Clear
			Set colQuickFixes = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering")
			For Each objQuickFix In colQuickFixes
				If Err.Number = 0 Then
					On Error GoTo 0
					If InStr(objQuickFix.HotFixID, "958644") Then
						out.WriteLine "Computer: " & objQuickFix.CSName & " YES" & VbCrLf
						qFound = True
					End If
				Else
					Err.Clear
					On Error GoTo 0
					out.WriteLine "Computer: " & strComputer & " WMI Error" & VbCrLf
				End If
			Next
			If Not qFound Then
				out.WriteLine "Computer: " & strComputer & " NO" & VbCrLf
			End If
		End If    
	Else
		out.WriteLine "Cannot ping " & strComputer & VbCrLf
	End If
Next 'For Each strComputer
 
Function Ping(strComputer)
    Dim objShell, boolCode
    Set objShell = CreateObject("WScript.Shell")
    boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
    If boolCode = 0 Then
        Ping = True
    Else
        Ping = False
    End If
End Function

Open in new window

Rob all fine but just on one exact machine the script stops and keeps reading the same machine and does not continue
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mtthompsons
mtthompsons

Hi Rob, Hope you are doing well..

I have the below script can you help add the installed date also to be recorded

'Const ADS_SCOPE_SUBTREE = 2
Const ForWriting = 2
 
'Set objConnection = CreateObject("ADODB.Connection")
'Set objCommand = CreateObject("ADODB.Command")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set out = objFSO.CreateTextFile("C:\out.txt", True)
 
inFile = "C:\computerlist.txt"
arrComputers = Split(objFSO.OpenTextFile(inFile).ReadAll, vbNewLine)
 
'objConnection.Provider = "ADsDSOObject"
'objConnection.Open "Active Directory Provider"
 
'Set objCOmmand.ActiveConnection = objConnection
'objCommand.CommandText = "Select Name, Location from 'LDAP://DC=co,dc=uk' " & "WHERE objectClass='computer' " & "and operatingSystemVersion = '5.1 (2600)'"
'objCommand.Properties("Page Size") = 1000
'objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
'Set objRecordSet = objCommand.Execute
'objRecordSet.MoveFirst
 
'Do Until objRecordSet.EOF
For Each strComputer In arrComputers
'        strComputer = objRecordSet.Fields("Name").Value
       qFound = false
        If Reachable(strComputer) Then
                On Error Resume Next
                Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                If Err.Number <> 0 Then
                        out.WriteLine "no permission On " & strComputer & vbCrLf
                Else
                        On Error GoTo 0
                End If
               
                Set colQuickFixes = objWMIService.ExecQuery _
                ("Select * from Win32_QuickFixEngineering")
                 
                If colQuickFixes Is Nothing Then
                    out.WriteLine "Computer: " & strComputer & " NO QuickFix records" & vbCrLf
                Else
                    For Each objQuickFix In colQuickFixes
                            If InStr(objQuickFix.HotFixID, "958644") Then
                                    out.WriteLine "Computer: " & objQuickFix.CSName & " YES" & vbCrLf
                                    qfound = true
                            End If
                    Next
                    if not qfound then out.WriteLine "Computer: " & strComputer & " NO" & vbCrLf
                End If
        Else
                out.WriteLine strComputer & " isnt reachble " & vbCrLf
        End If
        'objRecordSet.MoveNext
'Loop
Next 'For Each strComputer
 
 
Function Reachable(strComputer)
                         
        strCmd = "ping -n 1 " & strComputer
                         
        Set objShell = CreateObject("WScript.Shell")
        Set objExec = objShell.Exec(strCmd)
        strTemp = UCase(objExec.StdOut.ReadAll)
                         
        If InStr(strTemp, "REPLY FROM") Then
                Reachable = True
        Else
                Reachable = False
        End If
End Function