VBscript question re. setting default printer
-
Tuesday, August 07, 2012 9:35 PM
Hello everyone. I had a quick question regarding this script that I wrote. It's designed to run on login, find out if you are connected to any of the listed printers then replace that connection with a new printer. The printers are or the same server, same IP, different model. In any case, the script below works, however, I don't understand where it sets the correct default printer.
The logic to set the default printer is not even written into the script as far as I could tell, however, the default it being set correctly. I just need to understand how it's doing that. Thanks in advance for your help. I'm a newb script guy btw so any changes to make it flow better would be much appreciated. Thanks.
strComputer = "."
'New Printers Note: this is actual share path.
atl2k3vs04_print40 = "\\atl2k3vs04\35039 - Canonc7065"
atl2k3vs04_print41 = "\\atl2k3vs04\35039 - Canon8105"
atl2k3vs04_print42 = "\\atl2k3vs04\35047 - Canon6075"
Delete_Printer1
Delete_Printer2
Delete_Printer3
Function Delete_Printer1
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set printer1 = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Name like '%35039 - Canon5185%'")
For Each objPrinter in printer1
objPrinter.Delete_
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection atl2k3vs04_print40
Next
End Function
Function Delete_Printer2
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set printer2 = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Name like '%35039 - Canon7105%'")
For Each objPrinter in printer2
objPrinter.Delete_
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection atl2k3vs04_print41
Next
End Function
Function Delete_Printer3
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set printer3 = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Name like '%35047 - Canon5075%'")
For Each objPrinter in printer3
objPrinter.Delete_
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection atl2k3vs04_print42
Next
End Function
All Replies
-
Tuesday, August 07, 2012 10:04 PM
It may be a coincidence that it is setting the correct printer as default. Try changing the order of your functions and see if that is still the case.
Since you are already using WMI you can check if the printer is the default when you remove it. objPrinter.Default
So if it is important to make sure that you set the same one back. You may want to check for default and then use the setdefaultprinter (objNetwork.setdefaultprinter printerName) method inside the Delete_Printer function that corresponds to that printer. After you add the new one that is.
Mike
- Edited by mikem1983 Tuesday, August 07, 2012 10:22 PM
- Proposed As Answer by jrvMicrosoft Community Contributor Tuesday, August 07, 2012 10:41 PM
- Marked As Answer by IamMredMicrosoft Employee, Owner Wednesday, August 08, 2012 6:10 PM
-
Tuesday, August 07, 2012 10:46 PM
Mike isa onto something here.
I believe that it is the first printer that is the default if no other printers have been assigned as a default.
THink about it theis way. - if ther eare no printers then there are no defaults. One printer has to be the default. You cannot have a condition of no default printers except when theri are no printers. The first printer gets elected and all other printers just get on with it.
Of course it may be the last printer because WMI is like that.
It cannot be any middle printer. Can you guess why?
¯\_(ツ)_/¯
-
Wednesday, August 08, 2012 12:07 PM
What's really odd is that if I chose the first "old" printer as the default, the "new" replacement printer becomes the default, just as intended. And so on for the second and the third. If I chose another printer as the default, it leaves it alone, again as intended.
Basically, I'm trying to replace an old model printer with the new model on the same server.
I also need to take into consideration the fact that the default might be one of the old printers. I'm already taking care of replacing old printer with new with the delete_printer function, however, if I do a Set objNetwork = CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection then how do I build logic into it checking to see if it is one of the old ones, replace it with a new one and set it as default? I've used the script below to check for default printer, however, I can't seem to get a value out of the Get_Default_Printer. If I try to do a wscript.echo Get_Default_Printer it comes up blank.
Function Get_Default_Printer
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Default = TRUE")
For Each objPrinter in colPrinters
Get_Default_Printer = objPrinter.Name
Next
End Function -
Wednesday, August 08, 2012 12:46 PM
Looks like this script works and has the logic built into it to account for the default printer possibly being one of the printers needing to be replaced:
Thanks everyone for your help with this problem. Much appreciated!
strComputer = "."
'Old Printers Note: this is actual share path.
atl2k3vs04_OLDprint40 = "\\atl2k3vs04\35039 - Canon5185"
atl2k3vs04_OLDprint41 = "\\atl2k3vs04\35039 - Canon7105"
atl2k3vs04_OLDprint42 = "\\atl2k3vs04\35047 - Canon5075"
'New Printers Note: this is actual share path.
atl2k3vs04_print40 = "\\atl2k3vs04\35039 - Canonc7065"
atl2k3vs04_print41 = "\\atl2k3vs04\35039 - Canon8105"
atl2k3vs04_print42 = "\\atl2k3vs04\35047 - Canon6075"
Default_Printer = Get_Default_Printer
Delete_Printer1
Delete_Printer2
Delete_Printer3
Set_Default_Printer
Function Get_Default_Printer
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Default = TRUE")
For Each objPrinter in colPrinters
Get_Default_Printer = objPrinter.Name
Next
End Function
Function Delete_Printer1
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set printer1 = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Name like '%35039 - Canon5185%'")
For Each objPrinter in printer1
objPrinter.Delete_
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection atl2k3vs04_print40
Next
End Function
Function Delete_Printer2
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set printer2 = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Name like '%35039 - Canon7105%'")
For Each objPrinter in printer2
objPrinter.Delete_
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection atl2k3vs04_print41
Next
End Function
Function Delete_Printer3
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set printer3 = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Name like '%35047 - Canon5075%'")
For Each objPrinter in printer3
objPrinter.Delete_
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection atl2k3vs04_print42
Next
End Function
Function Set_Default_Printer
Set objNetwork = CreateObject("WScript.Network")
If Default_Printer = atl2k3vs04_OLDprint40 Then
objNetwork.SetDefaultPrinter atl2k3vs04_print40
End If
If Default_Printer = atl2k3vs04_OLDprint41 Then
objNetwork.SetDefaultPrinter atl2k3vs04_print41
End If
If Default_Printer = atl2k3vs04_OLDprint42 Then
objNetwork.SetDefaultPrinter atl2k3vs04_print42
End If
End Function

