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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Default Printer via Script.

Status
Not open for further replies.

agiddens

Technical User
Dec 29, 2004
5
GB
I want to use the standard add printer and set as default script to add a printer to a group of users. How can i add a IF statement so is a user belongs to a certian Group they get one printer and another group would get another printer..?

I am using:

Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddWindowsPrinterConnection "\\server\printer"
WshNetwork.SetDefaultPrinter "\\server\printer"

Anyone know how to make the IF statement?

 
Oops, see reply on solo7's thread.

Marty
Network Admin
Hilliard Schools
 
Thanks, I am using a VBS works a treat.. can do mapped drives etc as well.. Great!


' Mount Street Holdings - Deploy Network Printers by Group.
' Written By Adam Giddens agiddens@mshplc.com, 06/01/05
'
Dim WSHNetwork
Dim FSO
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs

Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
'
' Wait until the user is really logged in...
'
strUserName = ""
While strUserName = ""
WScript.Sleep 100 ' 1/10 th of a second
strUserName = WSHNetwork.UserName
Wend
strUserDomain = WSHNetwork.UserDomain

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object.

Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)
If MemberOf(ObjGroupDict, "General Floor Printer Users") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen001"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen002"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen003"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen004"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen005"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen006"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen007"
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen008"

End If

If MemberOf(ObjGroupDict, "HDB-P-GEN001") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen001"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen001"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN002") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen002"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen002"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN003") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen003"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen003"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN004") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen004"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen004"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN005") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen005"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen005"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN006") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen006"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen006"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN007") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen007"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen007"

ElseIf MemberOf(ObjGroupDict, "HDB-P-GEN008") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hdb-p-gen008"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hdb-p-gen008"

End If

If MemberOf(ObjGroupDict, "HDB-P-IT001") Then
WshNetwork.AddWindowsPrinterConnection "\\hdb-sfl-01\hbd-p-it001"
WshNetwork.SetDefaultPrinter "\\hdb-sfl-01\hbd-p-it001"

End If

Function MemberOf(ObjDict, strKey)
' Given a Dictionary object containing groups to which the user
' is a member of and a group name, then returns True if the group
' is in the Dictionary else return False.
'
' Inputs:
' strDict - Input, Name of a Dictionary object
' strKey - Input, Value being searched for in
' the Dictionary object
' Sample Usage:
'
' If MemberOf(ObjGroupDict, "DOMAIN ADMINS") Then
' wscript.echo "Is a member of Domain Admins."
' End If
'
'
MemberOf = CBool(ObjGroupDict.Exists(strKey))

End Function


Function CreateMemberOfObject(strDomain, strUserName)
' Given a domain name and username, returns a Dictionary
' object of groups to which the user is a member of.
'
' Inputs:
'
' strDomain - Input, NT Domain name
' strUserName - Input, NT username
'
Dim objUser, objGroup

Set CreateMemberOfObject = CreateObject("Scripting.Dictionary")
CreateMemberOfObject.CompareMode = vbTextCompare
Set objUser = GetObject("WinNT://" _
& strDomain & "/" _
& strUserName & ",user")
For Each objGroup In objUser.Groups
CreateMemberOfObject.Add objGroup.Name, "-"
Next
Set objUser = Nothing

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top