Question

Locked

Script to Add Printers based on if the target computers are in an OU

By chris.eagles ·
Hi,

Is it possible to write a script, most likely vbs that calls rundll32 printui.dll to add network printers to computers depending on which OU/Computer Group the computers are in?

I already use the above command to add printers to individual computer groups, but each room requires another policy, is there any way to consolidate all the policies into one, perhaps using if statements, if so I'm not a dstrong vbs scripter could somone give me an idea of the code required?

Thanks for any Help!

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

This one

by Wizard-09 In reply to Script to Add Printers ba ...

Should be able to help you understand what you need have a look, should be everything that you need for the moment.

http://www.microsoft.com/technet/scriptcenter/resources/qanda/jul07/hey0727.mspx

Edited to add

REM First lines install printers quietly.
REM Last line sets default printer.

rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Laser_1
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Photo_1
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Photo_2
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Label_3

rundll32 printui.dll,PrintUIEntry /y /n\\Server1\Photo_1

You could use something like this if you are using GPO would be alot easier for you, have it do the above as part as the log on script?

Just a thought :)

Keep us informed as to your progress if you require further assistance.

If you think that any of the posts that have been made by all TR Members, have solved or contributed to solving the problem, please Mark them as Helpful so that others may benefit from the outcome.

Collapse -

Well..

by chris.eagles In reply to This one

Thanks, however the problem is that if I do the above and add it to the root of AD then it will add all the same printers to ALL my computers. I am looking to: (Pseudo of the above)

if computergroup=Room1 then
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Laser_1
rundll32 printui.dll,PrintUIEntry /y /n\\Server1\Laser_1

else if computergroup=Room2 then
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Photo_1
rundll32 printui.dll,PrintUIEntry /y /n\\Server1\Photo_1

else if computergroup=Room3
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Photo_2
rundll32 printui.dll,PrintUIEntry /y /n\\Server1\Photo_2

else if computergroup=Room4
rundll32 printui.dll,PrintUIEntry /in /q /n\\Server1\Label_3
rundll32 printui.dll,PrintUIEntry /y /n\\Server1\Photo_3


Ah that could work, I'll look into the OU way for now

Thanks

Collapse -

vbscript

by scott_heath In reply to Script to Add Printers ba ...

Set oNetwork = CreateObject("WScript.Network")
DomainString = oNetwork.Domain
UserString = oNetwork.UserName

printer1 = "\\printsrv\printer1"
printer2 = "\\printsrv\printer2"
printer3 = "\\printsrv\printer3"

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
For Each GroupObj In UserObj.Groups
Select Case GroupObj.Name
Case "group1"
oNetwork.AddWindowsPrinterConnection printer1
oNetwork.SetDefaultPrinter printer1
Case "group2"
oNetwork.AddWindowsPrinterConnection printer2
oNetwork.SetDefaultPrinter printer2
Case "group3"
oNetwork.AddWindowsPrinterConnection printer3
oNetwork.SetDefaultPrinter printer3
End Select
Next

Enjoy,
Scott

http://scottisageek.spaces.live.com

Collapse -

Nearly...

by chris.eagles In reply to vbscript

Sorry I'm probably doing something wrong, this is the code I used below:

I keep getting a Windows Script Host Error or something, it says,

Line: 2
Char: 1
Error: Object doesn't support this property or method: 'oNetwork.Domain'
Code: 800A01B6
Source: Microsoft VBScript runtime error?

Set oNetwork = CreateObject("WScript.Network")
DomainString = oNetwork.Domain
UserString = oNetwork.UserName

printer1 = "\\nca-fp2\a2colourlaser-cp4005"
printer2 = "\\nca-fp2\a3colourlaser-cp4005"
printer3 = "\\nca-fp2\a4colourlaser-cp4005"

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
For Each GroupObj In UserObj.Groups
Select Case GroupObj.Name
Case "a2computers"
oNetwork.AddWindowsPrinterConnection printer1
oNetwork.SetDefaultPrinter printer1
Case "A3 Computers"
oNetwork.AddWindowsPrinterConnection printer2
oNetwork.SetDefaultPrinter printer2
Case "A4 Computers"
oNetwork.AddWindowsPrinterConnection printer3
oNetwork.SetDefaultPrinter printer3
End Select
Next

Help Please?

Collapse -

It Should read

by Wizard-09 In reply to Nearly...

This is wrong Error: Object doesn't support this property or method: 'oNetwork.Domain'

It should read

Domain = oNetwork.UserDomain

Hope this helps

Collapse -

The Error is gone but...

by chris.eagles In reply to It Should read

Well the error has gone, but no printers appear in the printers & Faxes, that includes if I run the script manually from within XP and not as a logon script,

If I adapt the above code to use the rundll32 printui.dll printentry command, could it be executed within the vbs script without calling a second .bat, because I'm trying keep this to one script file.

Thanks again

Collapse -

Yes, Kind of

by scott_heath In reply to The Error is gone but...

You can create and object like this:

Set oShell = CreateObject("WScript.Shell")
oShell.Run "c:\dir\blah.exe"

Try typing things like Wscript.Shell and WScript.Network in Google and you'll find the MSDN pages that explain all the great methods and properties in detail.

Sorry about the typo on the first script, I'm going to run a quick test on my PC and see what happens.

Regards,
Scott

http://scottisageek.spaces.live.com

Collapse -

Here it is

by scott_heath In reply to The Error is gone but...

I ran this script on two boxes and it set up my printer perfectly. I have changed nothing but the server name. As you can see, I am a "DBA User". :)

I ran this on Windows 7 RC and Windows XP Pro SP3. Be aware that group names are case sensitive. if you want to see exactly what is returned, add MsgBox GroupObj.Name after the For Each statement.

Set oNetwork = CreateObject("WScript.Network")
DomainString = oNetwork.UserDomain
UserString = oNetwork.UserName

printer1 = "\\printsrv\canonIR_c4850"
printer2 = "\\printsrv\printer2"
printer3 = "\\printsrv\printer3"

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
For Each GroupObj In UserObj.Groups
Select Case GroupObj.Name
Case "DBA Users"
oNetwork.AddWindowsPrinterConnection(printer1)
oNetwork.SetDefaultPrinter(printer1)
Case "group2"
oNetwork.AddWindowsPrinterConnection printer2
oNetwork.SetDefaultPrinter printer2
Case "group3"
oNetwork.AddWindowsPrinterConnection printer3
oNetwork.SetDefaultPrinter printer3
End Select
Next

Back to Desktop Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums