NetAdmin2436
asked on
Set default printer on this vbs script
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.Netw ork")
Set wshNet = CreateObject("WScript.Netw ork")
Set wshPrn = wshNet.EnumPrinterConnecti ons
For x = 0 To wshPrn.Count - 1 Step 2
If Left(wshPrn.Item(x+1),2) = "\\" Then wshNet.RemovePrinterConnec tion wshPrn.Item(x+1),True,True
Next
On Error Resume Next
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server\HPLaserJet5000PS "
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server00\HPColorLaserJe t4600PCL5c "
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server\Canon iR1600-2000 (FAX)"
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server\Canon iR1600-2000 PCL5e"
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server\HP DesignJet 800 24 by HP"
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server\hpLaserJet1300_P CL5e"
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
Set objWSHNetwork = CreateObject("WScript.Netw ork") 'create network object
strConnectString = "\\Server\HP LaserJet 6L"
strResult = objWSHNetwork.AddWindowsPr interConne ction(strC onnectStri ng)
************************** ********** ********** ********** ********** ********
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=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")
For Each objPrinter in colInstalledPrinters
objPrinter.SetDefaultPrint er()
Next
************************** ********** ********** ********** ********** ********** **
What do i add (and where) in script 1 to make the default printer the HPColorLaserJet4600PCL5c using the EnumPrinterConnections technique ?
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.Netw
Set wshNet = CreateObject("WScript.Netw
Set wshPrn = wshNet.EnumPrinterConnecti
For x = 0 To wshPrn.Count - 1 Step 2
If Left(wshPrn.Item(x+1),2) = "\\" Then wshNet.RemovePrinterConnec
Next
On Error Resume Next
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server\HPLaserJet5000PS
strResult = objWSHNetwork.AddWindowsPr
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server00\HPColorLaserJe
strResult = objWSHNetwork.AddWindowsPr
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server\Canon iR1600-2000 (FAX)"
strResult = objWSHNetwork.AddWindowsPr
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server\Canon iR1600-2000 PCL5e"
strResult = objWSHNetwork.AddWindowsPr
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server\HP DesignJet 800 24 by HP"
strResult = objWSHNetwork.AddWindowsPr
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server\hpLaserJet1300_P
strResult = objWSHNetwork.AddWindowsPr
Set objWSHNetwork = CreateObject("WScript.Netw
strConnectString = "\\Server\HP LaserJet 6L"
strResult = objWSHNetwork.AddWindowsPr
**************************
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=imper
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")
For Each objPrinter in colInstalledPrinters
objPrinter.SetDefaultPrint
Next
**************************
What do i add (and where) in script 1 to make the default printer the HPColorLaserJet4600PCL5c using the EnumPrinterConnections technique ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
There was an extra " in the 7th line, but i deleted it and ran the script again.
Rock on! Worked like a charm.....I hope i never have to manually add another printer to a workstation ever again when a printer gets moved.
Thanks Very Much!!!
Rock on! Worked like a charm.....I hope i never have to manually add another printer to a workstation ever again when a printer gets moved.
Thanks Very Much!!!
oops, yeah there was. Glad it worked for you.
<script>
strComputer = "."
Set objWMIService = GetObject("winmgmts:{imper
Set colInstalledPrinters = objWMIService.ExecQuery("S
objNet = CreateObject("Wscript.Netw
strHost = "\\"
For Each objPrinter in colInstalledPrinters
strPrinter = UCase(objPrinter.name)
If l(strPrinter,2) = strHost then
objNet.RemovePrinterConnec
end if
next
<end script>