?
Solved

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

Posted on 2012-04-04
19
Medium Priority
?
870 Views
Last Modified: 2012-04-04
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
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 6
19 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37804880
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
 
LVL 2

Author Comment

by:detox1978
ID: 37804885
how do I get it to save the results to output.txt
0
 
LVL 2

Author Comment

by:detox1978
ID: 37804914
I get an error on line 13
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37804929
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
 
LVL 2

Author Comment

by:detox1978
ID: 37804930
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37804964
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37804965
just create text file Result.txt in your c drive root. which will get the output.
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37804998
Detox1978 keep posted when you are done.. i tested and everything seems to be working.
0
 
LVL 2

Author Comment

by:detox1978
ID: 37805366
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805374
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805384
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805388
Please combine the code above and script will be good enough...
0
 
LVL 2

Author Comment

by:detox1978
ID: 37805453
Sorry i dont know how to combine them....
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805459
ok. give me few minutes.. then we will close this...:)
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805525
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805530
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
 
LVL 17

Accepted Solution

by:
Anuroopsundd earned 2000 total points
ID: 37805535
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
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37805537
last code will also write in the result about the list of machine that was not able to ping,
0
 
LVL 2

Author Closing Comment

by:detox1978
ID: 37805765
great thanks...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

777 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