Solved

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

Posted on 2012-04-04
19
850 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

12 Experts available now in Live!

Get 1:1 Help Now