Click here to Skip to main content
15,886,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a script that I have found and modified to fit my needs but I can not seem to get it to run properly. I need to grab the name of 2 printers, their model numbers, along with their driver name and revision from several machines on my network. This will be my first attempt at coding in VB and I have not had any coding experience in ~10 years. Any suggestions would be greatly aprreciated!

VB
On Error Resume Next

Const ForAppending = 8 
Const ForReading = 1 

Dim WshNetwork, objPrinter, intDrive, intNetLetter, objDriver
arrComputers = Array("here is where the list of computers goes")

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


For Each strComputer In arrComputers

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

filOutput = varUserName & ".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 "Name: "& (objPrinter.Name) 
For Each objDriver in colInstalledDrivers
strTest = Left(objDriver.Name, 2)
objOutputFile.WriteLine "Driver Name: " &(objDriver.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
Next
Next
Posted

1 solution

I think all this stuff needs to go inside the For..Next loop:

VB
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer",,48)
Set colInstalledDrivers =  objWMIService.ExecQuery("Select * from Win32_PrinterDriver",,48)
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)


?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900