troubleshooting Question

Set default printer on this vbs script

Avatar of NetAdmin2436
NetAdmin2436Flag for United States of America asked on
ProgrammingWindows Batch
4 Comments1 Solution2406 ViewsLast Modified:
Hey guys,
I'm not good at scripting. I've read that EnumPrinterConnections is a difficult technique.  But i found this script that will go through and delete all network printers and add network printers. Makes it nice if a printer get's changed. I added a call to my login script to start my PrinterScript.vbs when a user logs in. It works awesome...one problem though. The default printer. What do i add (and where) in script 1 to make the default printer the HPColorLaserJet4600PCL5c ?

script 1
********************************************************************************************
Set WshNetwork = CreateObject("WScript.Network")
Set wshNet = CreateObject("WScript.Network")
Set wshPrn = wshNet.EnumPrinterConnections
For x = 0 To wshPrn.Count - 1 Step 2
    If Left(wshPrn.Item(x+1),2) = "\\" Then wshNet.RemovePrinterConnection wshPrn.Item(x+1),True,True
Next
On Error Resume Next
Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\HPLaserJet5000PS"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server00\HPColorLaserJet4600PCL5c"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\Canon iR1600-2000 (FAX)"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\Canon iR1600-2000 PCL5e"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\HP DesignJet 800 24 by HP"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\hpLaserJet1300_PCL5e"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Server\HP LaserJet 6L"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)
 
**************************************************************************

I know i can make script 2 with the following to specify a default printer, just thought it would be easier and cleaner if it was just one script:

script 2
******************************************************************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")

For Each objPrinter in colInstalledPrinters
    objPrinter.SetDefaultPrinter()
Next
******************************************************************************

What do i add (and where) in script 1 to make the default printer the HPColorLaserJet4600PCL5c using the EnumPrinterConnections technique ?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros