Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

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

0
bsharath
Asked:
bsharath
1 Solution
 
Robberbaron (robr)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.
0
 
bsharathAuthor Commented:
Any way via this script
0
 
RobSampsonCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bsharathAuthor 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  
---------------------------
0
 
RobSampsonCommented:
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
 
bsharathAuthor 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  
---------------------------
0
 
RobSampsonCommented:
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
 
bsharathAuthor Commented:
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
 
bsharathAuthor Commented:
Rob all fine but just on one exact machine the script stops and keeps reading the same machine and does not continue
0
 
RobSampsonCommented:
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
 
mtthompsonsCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now