Windows Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsWindows Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 10th, 2008, 10:53 PM
gunk gunk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 1 gunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 53 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old July 23rd, 2008, 06:47 AM
The Cat The Cat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 1 The Cat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 24 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsWindows Help > Using setprinter.exe with vbs


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT