0

I'm trying to create a script which will enable view every PC to Printer relationship across an entire domain of around 1200 machines. In theory I am looking for a script to be run so that it will publish all the information it finds into a text document so that we can use this to view which PCs are connected to which printers (Network/Local).

I am currently using a script (Listed Below) which requires me to enter the IP or PC name of the computer so that it can query that PC and provide the current PC Printer relationship of the current logged on user.

I am hoping to change this script with some help from someone so that it will query a collection of PCs based within a textfile or CSV.

Const ForAppending = 8 
Const ForReading = 1 
Dim objTextFile
Dim objWriteFile

Dim objCopyFSO
Dim objWriteFSO
Dim objCrisFSO

Dim StrNextLine
Dim PCNameList
Dim FolderCopyError

Const ForWriting = 2
Const adTypeBinary = 1
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Const adModeReadWrite = 3



Dim sTitle, WshNetwork, objPrinter, intDrive, intNetLetter

'Tool Bar title
sTitle = "List Printers"
Dim StrComputer

'Textbox which enables the user to enter the IP/PC name of the PC
strComputer = InputBox("Enter IP or name of computer to check for " & _
                   "printer list (leave blank to check " & _
                   "local system)." & vbcrlf & vbcrlf & "Remote " & _
                   "checking only from NT type OS to NT type OS " & _
                   "with same Admin level UID & PW", sTitle)

'If the textbox is left empty then it will exit the application
If IsEmpty(strComputer) Then WScript.Quit
strComputer = Trim(strComputer)
If strComputer = "" Then strComputer = "."



 'Copy the files to each workstation
 'Create a new file and rename here to run multiple instances of this script.


Set WshNetwork = CreateObject("WScript.Network") 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") 
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) 
Set WshShell = WScript.CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 

For Each objItem in colItems 
UserName = objItem.UserName 
arrUserName = Split(UserName, "\", -1, 1) 
varUserName = arrUserName(1) 
Next 

filOutput = varUserName & "_printers.txt" 

If objFSO.FileExists(filOutput) Then 
objFSO.DeleteFile(filOutput) 
End If 

Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True) 
For Each objPrinter in colInstalledPrinters 
strTest = Left(objPrinter.Name, 2) 
objOutputFile.WriteLine(objPrinter.Name) 
Next 
'objOutputFile.Close


'added
Set objPrinter = WshNetwork.EnumPrinterConnections
'Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True) 
If objPrinter.Count = 0 Then
WScript.Echo "No Printers Mapped "
else
For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter +1
printer = "UNC Path " & objPrinter.Item(intDrive) & " = " & objPrinter.Item(intDrive +1) & " Printer : " & intDrive
objOutputFile.WriteLine(printer)
Next
end if
objOutputFile.Close
'added



varOpen = MsgBox("Do you want to view the printers?",36,"View File?") 
If varOpen = vbYes Then 
varCommand = "notepad " & filOutput 
WshShell.Run varCommand,1,False 
End If 

Wscript.Sleep 1500 
MsgBox "Printer mappings have been stored in '" & filOutput & "'.", 64, "Script Complete" 
Wscript.Quit

This is the original link for the script: http://community.spiceworks.com/scripts/show/1145-list-printers-vbs-update

If anyone has any experience doing something similar to this, or been in a situation where you've needed to collect Printer to PC relationship data, I'd be grateful for your input on how best to combat this issue.

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Wigleys_Extra
  • 23
  • 1
  • 5
  • `a collection of PCs based within a textfile or CSV` <-- eeewww. PowerShell and `Get-ADComputer` or just slap it into a GPO, and done. – HopelessN00b Dec 16 '14 at 16:58
  • haha! I would much prefer to use PowerShell. Would it be possible to still use PowerShell even if a large amount of PCs do not actually have any PowerShell components? – Wigleys_Extra Dec 16 '14 at 17:00
  • No, but now you have an excuse to deploy the latest version (or highest version for the OS) of WMF to all your domain computers. Makes your life *so* much easier, you'll wonder why you didn't do it before. – HopelessN00b Dec 16 '14 at 17:03
  • It would be a good excuse to use SCCM for this! However whether I can convince him or not is a different story. I will update the post with a more relevant script if I have created one, or what steps were taken. – Wigleys_Extra Dec 16 '14 at 17:05
  • Good luck with that - the advantage of WMF is that it's free. – HopelessN00b Dec 16 '14 at 17:07
  • My suggestion is to use OpenAudit free version, running as a logon script via GPO – Danilo Brambilla Dec 23 '14 at 16:26
  • Thank you! I haven't heard of OpenAudit before but it sounds promising, I will give it a go and let you know how I get on after Christmas. – Wigleys_Extra Dec 24 '14 at 10:01

0 Answers0