Solved

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

Posted on 2012-04-04
19
863 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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