Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting local printer default after mapping network printers

Status
Not open for further replies.

stre1026

IS-IT--Management
Jul 9, 2001
40
US
Hello all, I have written a VB Script that maps network printers on a print server to computers depending on their Organizational Unit in Active Directory and sets the appropriate mapped printer as default. This works great, however the problem comes when a user has a local printer attached. It always sets the network printer default. Is there any way to add code to my script that will detect a local printer and set that one default but also still map the network printers so they can be chosen in case of local printer problems, etc.?

The script is below. Thanks in advance for any help you can give me!

Dim objNetwork, ADSysInfo, ComputerName

Set objNetwork = CreateObject("Wscript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set ComputerName = GetObject("LDAP://" & ADSysInfo.ComputerName)

If Instr(ADSysInfo.ComputerName, "OU=IT Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\it"
objNetwork.SetDefaultPrinter("\\serverdc\it")
objNetwork.AddWindowsPrinterConnection "\\serverdc\server"

ElseIf Instr(ADSysInfo.ComputerName, "OU=News Computers") Then

objNetwork.SetDefaultPrinter("\\serverdc\draft")
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset1"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset2"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset3"
objNetwork.AddWindowsPrinterConnection "\\serverdc\desk"
objNetwork.AddWindowsPrinterConnection "\\serverdc\user1"

If Instr(ADSysInfo.ComputerName, "OU=Sports") Then

objNetwork.SetDefaultPrinter("\\serverdc\sports")

End If

ElseIf Instr(ADSysInfo.ComputerName, "OU=Sales Computers") Then

objNetwork.SetDefaultPrinter("\\serverdc\sales")
objNetwork.AddWindowsPrinterConnection "\\serverdc\xerox"

ElseIf Instr(ADSysInfo.ComputerName, "OU=Business Office Computers") Then

objNetwork.SetDefaultPrinter("\\serverdc\business")
objNetwork.AddWindowsPrinterConnection "\\serverdc\check"
objNetwork.AddWindowsPrinterConnection "\\serverdc\xerox"
objNetwork.AddWindowsPrinterConnection "\\serverdc\business color laser"

ElseIf Instr(ADSysInfo.ComputerName, "OU=Promotions Computers") Then

objNetwork.SetDefaultPrinter("\\serverdc\promo")
objNetwork.AddWindowsPrinterConnection "\\serverdc\draft"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset1"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset2"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset3"


End If

Set objNetwork = Nothing
Set ADSysInfo = Nothing
Set ComputerName = Nothing
Wscript.Quit
 
Yes, you will need to enumerate the installed printers. Look to my faq faq329-5798 for an example. Specifically look at the section for deleting network printers. You can modify this section.

Note: If the user has 2 local printers as may be the case with a notebook (home and work local printers) then you may be in trouble with your logic.

I hope you find this post helpful.

Regards,

Mark
 
Hi Markdmac,

Thanks for your suggestion. The machines that this script will run on do not have more than one local printer (Although, some have Adobe PDF printers so I guess that could be considered a local printer). They are all in a business environment.
I looked through your faq and have to say it is quite impressive. However, I can't figure out how to modify your deleting printers section to do what I want to do. How does the script know which is local and which isn't, etc.? I am not an expert at VB Scripting. I just poke away until it works :) So if you could help me code a solution I would greatly appreciate it!
 
Code:
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'Find local printers
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) <> "\\" Then
      WSHNetwork.SetDefaultPrinter _              
              (WSHPrinters.Item(LOOP_COUNTER +1))
    End If
Next

I hope you find this post helpful.

Regards,

Mark
 
PS, the Adobe printer WILL give you trouble with this.

I hope you find this post helpful.

Regards,

Mark
 
Thanks again, Markdmac....

I will give this code a go when I get into the office tomorrow.

Steve

PS> I have a solution for the Adobe Printer problem. I'll just get rid of them :)
 
Mark,

I tried adding your code to my script and it didn't seem to do anything. The local printer was not set as default. Maybe I didn't change something that needs to be changed?

Here is my script with your code added...

Thanks!
Steve

Dim objNetwork
Dim objPrinters
Dim ADSysInfo
Dim ComputerName

on error resume next

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
Set ADSysInfo = CreateObject("ADSystemInfo")
Set ComputerName = GetObject("LDAP://" & ADSysInfo.ComputerName)

If Instr(ADSysInfo.ComputerName, "OU=IT Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\it"
objNetwork.AddWindowsPrinterConnection "\\serverdc\server"
objNetwork.SetDefaultPrinter("\\serverdc\it")

ElseIf Instr(ADSysInfo.ComputerName, "OU=News Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\draft"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset1"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset2"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset3"
objNetwork.AddWindowsPrinterConnection "\\serverdc\desk"
objNetwork.AddWindowsPrinterConnection "\\serverdc\user1"
objNetwork.SetDefaultPrinter("\\serverdc\draft")

If Instr(ADSysInfo.ComputerName, "OU=Sports") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\sports"
objNetwork.SetDefaultPrinter("\\serverdc\sports")

End If

ElseIf Instr(ADSysInfo.ComputerName, "OU=Sales Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\sales"
objNetwork.AddWindowsPrinterConnection "\\serverdc\xerox"
objNetwork.SetDefaultPrinter("\\serverdc\sales")

ElseIf Instr(ADSysInfo.ComputerName, "OU=Business Office Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\business"
objNetwork.AddWindowsPrinterConnection "\\serverdc\check"
objNetwork.AddWindowsPrinterConnection "\\serverdc\xerox"
objNetwork.AddWindowsPrinterConnection "\\serverdc\business color laser"
objNetwork.SetDefaultPrinter("\\serverdc\business")

ElseIf Instr(ADSysInfo.ComputerName, "OU=Promotions Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\promo"
objNetwork.AddWindowsPrinterConnection "\\serverdc\draft"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset1"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset2"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset3"
objNetwork.SetDefaultPrinter("\\serverdc\promo")

End If


Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'Find local printers
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) <> "\\" Then
WSHNetwork.SetDefaultPrinter _
(WSHPrinters.Item(LOOP_COUNTER +1))
End If
Next
 
You did not change WSHNetwork to objnetwork to match your code for the Wscript.Network.

Change to this to match your code:

Code:
Set WSHPrinters = [red]obj[/red]Network.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'Find local printers
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) <> "\\" Then
      [red]obj[/red]Network.SetDefaultPrinter _              
              (WSHPrinters.Item(LOOP_COUNTER +1))
    End If
Next



I hope you find this post helpful.

Regards,

Mark
 
Hi Mark,

I knew it had to be something simple like that! I am not at work again so I will test it when I get in tomorrow (yes, I work weekends..the life of an IT person I guess :( )

Thanks again!
Steve
 
yes, I work weekends
Dude, you are seriously preaching to the choir. On call 24/7/365 for the past 12 years or so with many a 7 day work week. [sadeyes]

I hope you find this post helpful.

Regards,

Mark
 
That worked great! Thanks for all of your help!

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top