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
543 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)
Comment Utility
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
Comment Utility
Any way via this script
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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
 
LVL 11

Author Comment

by:bsharath
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Expert Comment

by:mtthompsons
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now