We help IT Professionals succeed at work.

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

Medium Priority
632 Views
Last Modified: 2015-01-30
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

Comment
Watch Question

CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Any way via this script
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
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  
---------------------------
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
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  
---------------------------
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
Hi Rob... Checking on this...
Can you have a look on this when time permits...

https://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_24545821.html

Author

Commented:
Rob all fine but just on one exact machine the script stops and keeps reading the same machine and does not continue
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.