|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Using setprinter.exe with vbs
First of all, I'll tell you the outcome I'm trying to acheive.
I need a way of setting the global and current user duplex option on every printer with 3030, 2020 or 3045 in it's name and I need to push it out via a logon script. It needs to work with absolutely no user interaction. Why? Because these settings need to be rolled out in an organisation with thousands of computers and hundreds of printers/copiers. setprinter.exe is from a microsoft resource kit and does almost exactly what I need it to, with one small problem, it doesn't support wildcards. The source of the batch file that can be used with setprinter.exe is below: REM [Set Global Defaults] setprinter "printername goes here" 8 "pdevmode=dmDuplex=2,dmCollate=1,dmFields=|duplex collate" REM [Set User Defaults] setprinter "printername goes here" 2 "pdevmode=dmDuplex=2,dmCollate=1,dmFields=|duplex collate" I also have a script which can retrieve printer names based on wildcards and it works fine: On Error Resume Next Const wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20 arrComputers = Array("computer name here") For Each strComputer In arrComputers Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_PrinterConfiguration Where DeviceName like '%3030%' or DeviceName like '%3045%' or DeviceName like '%2020%'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly) For Each objItem In colItems WScript.Echo "DeviceName: " & objItem.DeviceName WScript.Echo "Duplex: " & objItem.Duplex Next Next Now here is my almost final script: On Error Resume Next Const wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20 Set objShell = CreateObject("WScript.Shell") strComputer = "computer name here" Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_PrinterConfiguration Where DeviceName like '%3030%' or DeviceName like '%3045%' or DeviceName like '%2020%'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly) For Each objItem In colItems Set printerXYZ = objItem.DeviceName Set objScriptExec = objShell.Exec("setprinter " & Chr(34) & printerXYZ & Chr(34) & " 8 " & Chr(34) & "pdevmode=dmDuplex=2,dmCollate=1,dmFields=|duplex collate" & Chr(34)) WScript.Echo objScriptExec.StdOut.ReadAll Set objScriptExec = objShell.Exec("setprinter " & Chr(34) & printerXYZ & Chr(34) & " 2 " & Chr(34) & "pdevmode=dmDuplex=2,dmCollate=1,dmFields=|duplex collate" & Chr(34)) WScript.Echo objScriptExec.StdOut.ReadAll Next WScript.Echo "Done" If I replace printerXYZ with a hardcoded printer name it works fine, but every attempt at getting the name inserted in there so far has resulted in either a failed script because it tries to actually set the name as printerXYZ or &objItem.DeviceName& or something similar, or it sets duplex on every printer that is installed. Hopefully there is someone here has a lot more scripting knowledge than me and knows how I can fix this? |
|
#2
|
|||
|
|||
|
Remove the SET in Set printerXYZ = objItem.DeviceName
You only use SET to assign an object - you don't need to here, you just want to know the name of the printer so it's a standard variable assignment, i.e.
printerXYZ=objItem.DeviceName |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > Windows Help > Using setprinter.exe with vbs |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|