Tweak VBScript to run against computers.txt and write to output.txt

Hi All,

Can anyone help me so this script runs against a list of computer names in computers.txt and writes the results to output.txt


strComputer = "MyComputerName"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then 
        strPrinterType = "Local"
    Else
        strPrinterType = "Network"
    End If
    Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next

Open in new window


many thanks
LVL 2
detox1978Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AnuroopsunddConnect With a Mentor Commented:
On Error Resume Next

Const ForAppending = 8
Const ForReading = 1
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

Set oTS = oFS.OpenTextFile("C:\computers.txt", ForReading)
Set objTextFile = objFSO.OpenTextFile ("c:\Result.txt", ForAppending, True)

Do Until oTS.AtEndOfStream
strCompname = oTS.ReadLine
strPingStatus = PingStatus(strCompname)
If strPingStatus = "Success" Then

            Set objWMIService = GetObject("winmgmts:\\" & strCompname & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
      For Each objPrinter in colPrinters
                If objPrinter.Attributes And 64 Then
                    strPrinterType = "Local"
                 Else
                    strPrinterType = "Network"
                End If
    objTextFile.WriteLine objPrinter.Name & " -- " & strPrinterType
Next

Else
    'Machine unreachable.  Add code to log the name here
    objTextFile.writeline "Failure pinging Machine name -" & strCompname & ": " & strPingStatus
End If


Loop

Wscript.Echo "Script has run Succesfully. Please see C:\Result.txt file for results "


oTS.Close
objTextFile.close

Function PingStatus(strCompname)

    On Error Resume Next
   
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strCompname & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strCompname & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = _
              "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = _
              "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & objPing.StatusCode & _
               " - Unable to determine cause of failure."
        End Select
    Next

End Function
0
 
AnuroopsunddCommented:
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

 'Open a text file of computer names
'with one computer name per line
Set oTS = oFS.OpenTextFile("C:\computers.txt")

'go through the text file
Do Until oTS.AtEndOfStream
 'get the next computer name
 'store it in variable strCompname
strCompname = oTS.ReadLine
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then
        strPrinterType = "Local"
    Else
        strPrinterType = "Network"
    End If
    Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next

Loop
'close the text file
oTS.Close
0
 
detox1978Author Commented:
how do I get it to save the results to output.txt
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
detox1978Author Commented:
I get an error on line 13
0
 
AnuroopsunddCommented:
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTS = oFS.OpenTextFile("C:\computers.txt")
Do Until oTS.AtEndOfStream
strCompname = oTS.ReadLine
            Set objWMIService = GetObject("winmgmts:\\" & strCompname & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then
        strPrinterType = "Local"
     Else
        strPrinterType = "Network"
    End If
    Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next

Loop
oTS.Close
0
 
detox1978Author Commented:
Fixed the error there was a typo in the computer name variable

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

 'Open a text file of computer names
 'with one computer name per line
   Set oTS = oFS.OpenTextFile("computers.txt")

 'go through the text file
    Do Until oTS.AtEndOfStream

 'get the next computer name
 'store it in variable strCompname
   strComputer = oTS.ReadLine
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

   Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
   For Each objPrinter in colPrinters
      If objPrinter.Attributes And 64 Then 
          strPrinterType = "Local"
      Else
          strPrinterType = "Network"
      End If
      Wscript.Echo objPrinter.Name & " -- " & strPrinterType
   Next

   Loop
 'close the text file
oTS.Close

Open in new window

0
 
AnuroopsunddCommented:
Const ForAppending = 8
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFS.OpenTextFile("C:\computers.txt")
Set objTextFile = objFSO.OpenTextFile ("c:\Result.txt", ForAppending, True)
Do Until oTS.AtEndOfStream
strCompname = oTS.ReadLine
            Set objWMIService = GetObject("winmgmts:\\" & strCompname & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then
        strPrinterType = "Local"
     Else
        strPrinterType = "Network"
    End If
    objTextFile.WriteLine objPrinter.Name & " -- " & strPrinterType
Next

Loop
oTS.Close
objTextFile.close
0
 
AnuroopsunddCommented:
just create text file Result.txt in your c drive root. which will get the output.
0
 
AnuroopsunddCommented:
Detox1978 keep posted when you are done.. i tested and everything seems to be working.
0
 
detox1978Author Commented:
Thanks that works very well.

However when it tries to lookup a computer that's offline it stops.  Is there a way to have it skip to the next computer?
0
 
AnuroopsunddCommented:
you have to check the machine to be active before runnning you command.
put the ping command and if it is succefull then it should go ahead.
0
 
AnuroopsunddCommented:
http://www.tek-tips.com/faqs.cfm?fid=4871


strPingStatus = PingStatus(strComputer)
If strPingStatus = "Success" Then
    'Ping was good, do some work!
    '*****************************************
    'Worker section goes in here now.
    '*****************************************
Else
    'Machine unreachable.  Add code to log the name here
    Wscript.Echo "Failure pinging " & strComputer & ": " & strPingStatus
End If
0
 
AnuroopsunddCommented:
Please combine the code above and script will be good enough...
0
 
detox1978Author Commented:
Sorry i dont know how to combine them....
0
 
AnuroopsunddCommented:
ok. give me few minutes.. then we will close this...:)
0
 
AnuroopsunddCommented:
On Error Resume Next

Const ForAppending = 8
Const ForReading = 1
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

Set oTS = oFS.OpenTextFile("C:\computers.txt", ForReading)
Set objTextFile = objFSO.OpenTextFile ("c:\Result.txt", ForAppending, True)

Do Until oTS.AtEndOfStream
strCompname = oTS.ReadLine
strPingStatus = PingStatus(strCompname)
If strPingStatus = "Success" Then

            Set objWMIService = GetObject("winmgmts:\\" & strCompname & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
      For Each objPrinter in colPrinters
                If objPrinter.Attributes And 64 Then
                    strPrinterType = "Local"
                 Else
                    strPrinterType = "Network"
                End If
    objTextFile.WriteLine objPrinter.Name & " -- " & strPrinterType
Next

Else
    'Machine unreachable.  Add code to log the name here
    objTextFile.writeline "Failure pinging " & strComputer & ": " & strPingStatus
End If


Loop

Wscript.Echo "Script has run Succesfully. Please see C:\Result.txt file for results "


oTS.Close
objTextFile.close

Function PingStatus(strComputer)

    On Error Resume Next
   
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = _
              "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = _
              "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & objPing.StatusCode & _
               " - Unable to determine cause of failure."
        End Select
    Next

End Function
0
 
AnuroopsunddCommented:
now you will see results in C:\result.txt file and when the script is completed with the action will show up a prompt that script has run completely.
0
 
AnuroopsunddCommented:
last code will also write in the result about the list of machine that was not able to ping,
0
 
detox1978Author Commented:
great thanks...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.