Solved

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

Posted on 2009-06-30
12
549 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

0
Comment
Question by:bsharath
12 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 24753287
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 24762254
Any way via this script
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24782330
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

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 11

Author Comment

by:bsharath
ID: 24782676
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  
---------------------------
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24783051
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

0
 
LVL 11

Author Comment

by:bsharath
ID: 24783579
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  
---------------------------
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24790889
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

0
 
LVL 11

Author Comment

by:bsharath
ID: 24791119
Hi Rob... Checking on this...
Can you have a look on this when time permits...

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_24545821.html
0
 
LVL 11

Author Comment

by:bsharath
ID: 24794781
Rob all fine but just on one exact machine the script stops and keeps reading the same machine and does not continue
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 24799959
Where is that computer name in the text file? In the middle, or is the last one?  That really shouldn't happen....try 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 Trim(strComputer) <> "" Then
		If Ping(Trim(strComputer)) Then
			On Error Resume Next
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Trim(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
	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

0
 
LVL 11

Author Comment

by:bsharath
ID: 24800370
0
 

Expert Comment

by:mtthompsons
ID: 40579282
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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question