Page 1 of 1

VBS Default Printing Failing

Posted: Sat Nov 05, 2016 1:27 pm
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

Re: VBS Default Printing Failing

Posted: Sat Nov 05, 2016 2:10 pm
by jvierra
Try this:

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

Re: VBS Default Printing Failing

Posted: Sat Nov 05, 2016 2:25 pm
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?

Re: VBS Default Printing Failing

Posted: Sat Nov 05, 2016 2:26 pm
by jazz albert
As a layman it is difficult to do so on my own!

Re: VBS Default Printing Failing

Posted: Sat Nov 05, 2016 2:31 pm
by jvierra
Just put it in a file and run it. Use notepad or any other text editor.

Re: VBS Default Printing Failing

Posted: Sat Nov 05, 2016 2:46 pm
by jazz albert
Thank yo so much for your prompt response!

Re: VBS Default Printing Failing

Posted: Sun Nov 06, 2016 9:27 am
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?

Re: VBS Default Printing Failing

Posted: Sun Nov 06, 2016 11:45 pm
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
------------------------------------------------------------------------------------