We help IT Professionals succeed at work.

VBS script to gather printer info, need hlpe with server name and running remotely

Hello,
I have a script, I didn't write it from scratch. But I am trying to add code to it so that I can also get the name of the server that the queue is installed on along with the other info already in the script. This info is exported to a CSV file.

I tried adding code related to "Computer Name: " & objComputer.Name


The other thing I am trying to do is run the script remotely, so how would I change it so that I can run it on a list of server names located in a text file? Should I open another question?
Gather-Print-Data.txt
Comment
Watch Question

Author

Commented:
Ooops didn't finish one sentence.

I tried adding code related to "Computer Name: " & objComputer.Name but couldn't get it to work.

Commented:
Where it says computerName = "."  
Change it to computerName = strServerName

To populate serverName with Server Names from a text file use something similar to this:
On Error Resume Next
Const ForRead = 1    'Opens file for reading
Const ForWriting = 2 'Opens file for writing, overwriting file
Const ForAppend = 8  'Opens file for Appending
strComputer = "."    ' This computer

Const OverwriteExisting = True
Const NoOverWrite = False


' Open the input File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\ServerNames.txt", ForRead)
Err.Clear
Set ResultsFile=objFSO.OpenTextFile("C:\temp\results.txt", ForWriting )
If Err.Number <>0 Then
     Wscript.Echo "The results file could not be opened"
     Wscript.Echo "The errror number is " & Err.Number
     Wscript.Echo "The error message is " & Err.Message
End If
Wscript.Sleep 5000

Do Until objFile.AtEndOfStream
     'Read line from input file
     strServerName = objFile.ReadLine

Insert GetPrintData code Here


Loop

objfile.Close
ResultsFile.close

Author

Commented:
I know I'm doing something wrong, I can't seem to get it to add info for the server name. Would I want to modify the code below where it seems like the info it gathers is defined or is it something I have to add like a different statement?

For Each objPrinter in colInstalledPrinters
    
	strPortName = objPrinter.PortName
	strHostAddress = ""
	Get_Host_Address strPortName,strHostAddress
  	
  	strName = objPrinter.Name
  	strColor = "1"
  	strDuplex = "False"
  	Get_Printer_Options strName, strColor, strDuplex
  	
  	strStatusCode = objPrinter.ExtendedPrinterStatus
  	Get_Printer_Status strStatusCode, strStatus
    
    
    WScript.Echo "Name: " & strName
    WScript.Echo "IP Address:" & strHostAddress
    Wscript.Echo "Port Name: " & objPrinter.PortName
    Wscript.Echo "Status: " & strStatus
    Wscript.Echo "Driver: " & objPrinter.DriverName
    WScript.Echo "Location: " & objPrinter.Location
    Wscript.Echo "Comment: " & objPrinter.Comment
    Wscript.Echo "Shared: " & objPrinter.Shared
    Wscript.Echo "Published in AD: " & objPrinter.Published
    WScript.Echo "Color: " & strColor
    Wscript.Echo "Duplex: " & strDuplex
    Wscript.Echo ""
    Wscript.Echo ""
    
    strMessage = strName &","& strHostAddress &","& objPrinter.PortName  &","& strStatus &","& objPrinter.DriverName &","& objPrinter.Location &","& objPrinter.Comment &","& objPrinter.Shared &","& objPrinter.Published &","& strColor &","& strDuplex
	Write_Log_File strMessage

Open in new window

Commented:
Try this one.   I put it all together for you.

On Error Resume Next
Const ForRead = 1    'Opens file for reading
Const ForWriting = 2 'Opens file for writing, overwriting file
Const ForAppend = 8  'Opens file for Appending
strComputer = "."    ' This computer

Const OverwriteExisting = True
Const NoOverWrite = False


' Open the input File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\ServerNames.txt", ForRead)
Err.Clear
Set ResultsFile=objFSO.OpenTextFile("C:\temp\results.txt", ForWriting )
If Err.Number <>0 Then
     Wscript.Echo "The results file could not be opened"
     Wscript.Echo "The errror number is " & Err.Number
     Wscript.Echo "The error message is " & Err.Message
End If
Wscript.Sleep 5000

Do Until objFile.AtEndOfStream
     'Read line from input file
     strServerName = objFile.ReadLine

strMessage = "Name,IP Address,Port Name,Status,Driver,Location,Comment,Shared,Published in AD,Color(1 = BW/2 = Color),Duplex"
Write_Log_File strMessage

'= ***********************************************************************
'= Main
'= ***********************************************************************

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer")

On Error Resume Next
For Each objPrinter in colInstalledPrinters
   
      strPortName = objPrinter.PortName
      strHostAddress = ""
      Get_Host_Address strPortName,strHostAddress
        
        strName = objPrinter.Name
        strColor = "1"
        strDuplex = "False"
        Get_Printer_Options strName, strColor, strDuplex
        
        strStatusCode = objPrinter.ExtendedPrinterStatus
        Get_Printer_Status strStatusCode, strStatus
   
    WScript.Echo "Name: " & strName
    WScript.Echo "IP Address:" & strHostAddress
    Wscript.Echo "Port Name: " & objPrinter.PortName
    Wscript.Echo "Status: " & strStatus
    Wscript.Echo "Driver: " & objPrinter.DriverName
    WScript.Echo "Location: " & objPrinter.Location
    Wscript.Echo "Comment: " & objPrinter.Comment
    Wscript.Echo "Shared: " & objPrinter.Shared
    Wscript.Echo "Published in AD: " & objPrinter.Published
    WScript.Echo "Color: " & strColor
    Wscript.Echo "Duplex: " & strDuplex
    Wscript.Echo ""
    Wscript.Echo ""
   
    strMessage = strName &","& strHostAddress &","& objPrinter.PortName  &","& strStatus &","& objPrinter.DriverName &","& objPrinter.Location &","& objPrinter.Comment &","& objPrinter.Shared &","& objPrinter.Published &","& strColor &","& strDuplex
      Write_Log_File strMessage
Next

Loop

objfile.Close
ResultsFile.close    


'= ***********************************************************************
'= Get_Host_Address
'= ***********************************************************************
Function Get_Host_Address (strPortName,strHostAddress)


Set colInstalledPorts =  objWMIService.ExecQuery ("Select * from Win32_TCPIPPrinterPort Where Name = '" & strPortName & "'")

On Error Resume Next
For Each objPort in colInstalledPorts
          strHostAddress = objPort.HostAddress
Next

End Function

'= ***********************************************************************
'= Get_Printer_Options
'= ***********************************************************************
Function Get_Printer_Options(strName, strColor, strDuplex)

Set colPrinterOptions =  objWMIService.ExecQuery ("Select * from Win32_PrinterConfiguration Where Name = '" & strName & "'")

On Error Resume Next      
For Each objOption in colPrinterOptions
         strColor = objOption.Color
         strDuplex = objOption.Duplex
Next    

End Function

'= ***********************************************************************
'= Get_Printer_Status
'= ***********************************************************************
Function Get_Printer_Status(strStatusCode, strStatus)

Select Case strStatusCode

Case 1
      strStatus = "Other"
Case 2
      strStatus = "Unknown"
Case 3
      strStatus = "Idle"
Case 4
      strStatus = "Printing"
Case 5
      strStatus = "Warming Up"
Case 6
      strStatus = "Stopped Printing"  
Case 7
      strStatus = "Offline"  
Case 8
      strStatus = "Paused"  
Case 9
      strStatus = "Error"
Case 10
      strStatus = "Busy"  
Case 11
      strStatus = "Not Available"  
Case 12
      strStatus = "Waiting"  
Case 13
      strStatus = "Processing"
Case 14
      strStatus = "Initialization"
Case 15
      strStatus = "Power Save"
Case 16
      strStatus = "Pending Deletion"
Case 17
      strStatus = "I/O Active"
Case 18
      strStatus = "Manual Feed"
Case Else  
      strStatus = "Unknown"
End Select

End Function

'**********************************************************
'Write_Log_File
'**********************************************************
Function Write_Log_File (strMessage)

Dim objFso
Set objFso = CreateObject("Scripting.FilesystemObject")
 
Dim strScriptPath
strScriptPath = objFso.GetAbsolutePathName( WScript.ScriptFullname & "\.." )

Dim strOutputFile
strOutputFile = "Printer_Info.csv"


If (objFso.FileExists (strScriptPath &"\"& strOutputFile) = False) Then
      objFso.CreateTextFile strScriptPath &"\"& strOutputFile, True
End If

Dim oFiletxt
Set oFiletxt =  objFso.OpenTextFile (strScriptPath &"\"& strOutputFile,ForAppending,True)
oFiletxt.WriteLine(strMessage)
oFiletxt.Close

End Function

Author

Commented:
Hello,
I created a c:\temp folder and added a servernames and results text file. I added one remote server to the servernames file. When I run the script it seems like it runs and it creates the CSV file but it's empty.

For the server name I tried the short name and FQDN as well as the IP address.

Any other ideas what the problem is? I'm sure it's something simple I'm just not as experienced with VBS scripting.

Thanks, for your help.

Commented:
Try this one:
On Error Resume Next
Const ForRead = 1    'Opens file for reading
Const ForWriting = 2 'Opens file for writing, overwriting file
Const ForAppend = 8  'Opens file for Appending
strComputer = "."    ' This computer

Const OverwriteExisting = True
Const NoOverWrite = False


' Open the input File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\ServerNames.txt", ForRead)
Err.Clear
Set ResultsFile=objFSO.OpenTextFile("C:\temp\results.txt", ForWriting )
If Err.Number <>0 Then
     Wscript.Echo "The results file could not be opened"
     Wscript.Echo "The errror number is " & Err.Number
     Wscript.Echo "The error message is " & Err.Message
End If
Wscript.Sleep 5000

Do Until objFile.AtEndOfStream
     'Read line from input file
     strServerName = objFile.ReadLine

strMessage = "Name,IP Address,Port Name,Status,Driver,Location,Comment,Shared,Published in AD,Color(1 = BW/2 = Color),Duplex"
Write_Log_File strMessage

'= ***********************************************************************
'= Main
'= ***********************************************************************

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer")

On Error Resume Next
For Each objPrinter in colInstalledPrinters
   
      strPortName = objPrinter.PortName
      strHostAddress = ""
      Get_Host_Address strPortName,strHostAddress
       
        strName = objPrinter.Name
        strColor = "1"
        strDuplex = "False"
        Get_Printer_Options strName, strColor, strDuplex
       
        strStatusCode = objPrinter.ExtendedPrinterStatus
        Get_Printer_Status strStatusCode, strStatus
   
    WScript.Echo "Name: " & strName
    WScript.Echo "IP Address:" & strHostAddress
    Wscript.Echo "Port Name: " & objPrinter.PortName
    Wscript.Echo "Status: " & strStatus
    Wscript.Echo "Driver: " & objPrinter.DriverName
    WScript.Echo "Location: " & objPrinter.Location
    Wscript.Echo "Comment: " & objPrinter.Comment
    Wscript.Echo "Shared: " & objPrinter.Shared
    Wscript.Echo "Published in AD: " & objPrinter.Published
    WScript.Echo "Color: " & strColor
    Wscript.Echo "Duplex: " & strDuplex
    Wscript.Echo ""
    Wscript.Echo ""
   
    strMessage = strName &","& strHostAddress &","& objPrinter.PortName  &","& strStatus &","& objPrinter.DriverName &","& objPrinter.Location &","& objPrinter.Comment &","& objPrinter.Shared &","& objPrinter.Published &","& strColor &","& strDuplex
      Write_Log_File strMessage
Next

Loop

objfile.Close
ResultsFile.close    


'= ***********************************************************************
'= Get_Host_Address
'= ***********************************************************************
Function Get_Host_Address (strPortName,strHostAddress)


Set colInstalledPorts =  objWMIService.ExecQuery ("Select * from Win32_TCPIPPrinterPort Where Name = '" & strPortName & "'")

On Error Resume Next
For Each objPort in colInstalledPorts
          strHostAddress = objPort.HostAddress
Next

End Function

'= ***********************************************************************
'= Get_Printer_Options
'= ***********************************************************************
Function Get_Printer_Options(strName, strColor, strDuplex)

Set colPrinterOptions =  objWMIService.ExecQuery ("Select * from Win32_PrinterConfiguration Where Name = '" & strName & "'")

On Error Resume Next      
For Each objOption in colPrinterOptions
         strColor = objOption.Color
         strDuplex = objOption.Duplex
Next    

End Function

'= ***********************************************************************
'= Get_Printer_Status
'= ***********************************************************************
Function Get_Printer_Status(strStatusCode, strStatus)

Select Case strStatusCode

Case 1
      strStatus = "Other"
Case 2
      strStatus = "Unknown"
Case 3
      strStatus = "Idle"
Case 4
      strStatus = "Printing"
Case 5
      strStatus = "Warming Up"
Case 6
      strStatus = "Stopped Printing"  
Case 7
      strStatus = "Offline"  
Case 8
      strStatus = "Paused"  
Case 9
      strStatus = "Error"
Case 10
      strStatus = "Busy"  
Case 11
      strStatus = "Not Available"  
Case 12
      strStatus = "Waiting"  
Case 13
      strStatus = "Processing"
Case 14
      strStatus = "Initialization"
Case 15
      strStatus = "Power Save"
Case 16
      strStatus = "Pending Deletion"
Case 17
      strStatus = "I/O Active"
Case 18
      strStatus = "Manual Feed"
Case Else  
      strStatus = "Unknown"
End Select

End Function

'**********************************************************
'Write_Log_File
'**********************************************************
Function Write_Log_File (strMessage)

Dim objFso
Set objFso = CreateObject("Scripting.FilesystemObject")
 
Dim strScriptPath
strScriptPath = objFso.GetAbsolutePathName( WScript.ScriptFullname & "\.." )

Dim strOutputFile
strOutputFile = "Printer_Info.csv"


If (objFso.FileExists (strScriptPath &"\"& strOutputFile) = False) Then
      objFso.CreateTextFile strScriptPath &"\"& strOutputFile, True
End If


ResultsFile.WriteLine(strMessage)


End Function
Commented:
This will be the results file:
Set ResultsFile=objFSO.OpenTextFile("C:\temp\results.txt", ForWriting )



Scott

Commented:
Did this work for you?  I noticed a Powershell question was also opened by you on the same topic.   If this helped at all, please award the points and close the question.

Scott

Author

Commented:
Hello, sorry for the delay. It will export data to the results file but it still doesn't read from the servernames.txt file.

I got the power shell script sort of working but I need to ge tthe VBS script to work still.

Author

Commented:
Still wasn't able to get it to work right. I'll have to start working on this again later. Thanks for the help though.