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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
detox1978Author Commented:
I get an error on line 13
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.