VBS Default Printing Failing

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
devereux0110
Posts: 38
Last visit: Mon Oct 02, 2017 7:11 am

VBS Default Printing Failing

Post by devereux0110 »

We a basic VBS Script to add a printer and then set it as default.
Nothing special about the script and as Domain Admin Works perfectly.

However as a non domain admin, the script maps the printer and makes it available to the user for use with no problems but fails to set it as default.
What could stop the user from being able to execute the vbs to set the default print.

The user can go into devices and printers and set the printer default manually

As said, nothing special about the script, it works as administrator every time, it appears to be a permission issue but can't work out where or how.

Any suggestion's appreciated
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: VBS Default Printing Failing

Post by jvierra »

Try this:

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.SetDefaultPrinter "HP Printer"
User avatar
jazz albert
Posts: 16
Last visit: Sat Nov 05, 2016 3:09 pm

Re: VBS Default Printing Failing

Post by jazz albert »

jvierra wrote:Try this:

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.SetDefaultPrinter "HP Printer"
Would you please explain the insertion of this script with the help of visuals?
User avatar
jazz albert
Posts: 16
Last visit: Sat Nov 05, 2016 3:09 pm

Re: VBS Default Printing Failing

Post by jazz albert »

As a layman it is difficult to do so on my own!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: VBS Default Printing Failing

Post by jvierra »

Just put it in a file and run it. Use notepad or any other text editor.
User avatar
jazz albert
Posts: 16
Last visit: Sat Nov 05, 2016 3:09 pm

Re: VBS Default Printing Failing

Post by jazz albert »

Thank yo so much for your prompt response!
User avatar
devereux0110
Posts: 38
Last visit: Mon Oct 02, 2017 7:11 am

Re: VBS Default Printing Failing

Post by devereux0110 »

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.SetDefaultPrinter "HP Printer"

is basically the script we have
not at work at moment but it is the above with the extra line to add the printer first the set it as default.
We know the script works as when run as Domain Administrator it works, it doesn't work when logged on as normal user.

The printer is added to the client no problem, it just doesn't get set as default, even tried adding a loop to check after 5 seconds what the default printer was and then if not the one we where trying to make default, run the line WSHNetwork.SetDefaultPrinter "HP Printer" again and again but script just loops as it never manages to successful set it as default.

The user can print to the printer by selecting the printer from the options available and is able to manually set the printer as default in devices and printers, the script just seems to fail to do the job, what could stop such a simple script working?
User avatar
devereux0110
Posts: 38
Last visit: Mon Oct 02, 2017 7:11 am

Re: VBS Default Printing Failing

Post by devereux0110 »

Hello All - Update
It turned out there was nothing wrong with the script, it is an issue with setting a default printer in a Citrix Session.

Reverted to using printui.dll to set the default printer instead of the .SetDefaultPrinter option
Script below which includes check to make sure default sets and runs command again if it fails first time, sometimes caused by timing where on slow machine your printer may not have finished installing when you try set it as default
Script below between lines

------------------------------------------------------------------------------------------------------------------------------
Set WS = CreateObject("Wscript.Shell")
Set net = CreateObject("WScript.Network")

originaldefaultprinter = ""
requireddefaultprinter = "\\ServerName\PrinterName"
strComputer = "."

net.AddWindowsPrinterConnection requireddefaultprinter
WScript.Sleep 5000

Do Until originaldefaultprinter = requireddefaultprinter
' net.SetDefaultPrinter requireddefaultprinter
WScript.Sleep 5000
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Default = TRUE")
For Each objPrinter in colPrinters
originaldefaultprinter = objPrinter.Name
Next
WS.run "RUNDLL32.EXE printui.dll,PrintUIEntry /y /n " & requireddefaultprinter
Loop

WScript.Quit
------------------------------------------------------------------------------------
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked