Solved

Set default printer by Group Membership in AD (VB Script)

Posted on 2011-02-15
10
567 Views
Last Modified: 2014-01-01
Hey!

I am trying to create a simple script to set the default printer for users.
The users are spread to different locations and they would like to have their local network printers as default based on group membership

I have created several groups in AD by printername ("S-PrinterDefault-PRShareName") and added users to these groups.
In our exsisting VB script we have mapped all printers and I assume that I have to add this default line after this lines.

0
Comment
Question by:GMH77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34902907
Hi, here's another script you can use to map printers by group membership, and set one as the default printer.

In your case, if you already have all printers mapped, then just specify one printer per group, and use True as the "default flag".

Regards,

Rob.
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY
' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP Function
Dim objMemberships

' The array consists of these elements per line
'<Group Name>|<Printer Share>|<Default Flag>
' The Group Name can be ALL to map a printer for everyone
' and the default flag can be True, False, or omitted
' to set that printer as the default for that group
arrPrinters = Array( _
      "ALL|\\server1\CommonPrinter", _
      "Sydney-NO-2270MFD-Default|\\fs1\syndeymfd|True", _
      "Sydney-NO-2270MFD|\\fs1\sydneymfd" _
      )

Set objNetwork = CreateObject("WScript.Network") 
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Section to map the network printers
For Each strPrinterSpec In arrPrinters
	arrBits = Split(strPrinterSpec, "|")
	strGroup = arrBits(0)
	strPrinter = arrBits(1)
	If UBound(arrBits) = 2 Then
		blnDefault = arrBits(2)
		If LCase(blnDefault) = "true" Then
			blnDefault = True
		ElseIf LCase(blnDefault) = "false" Then
			blnDefault = False
		Else
			blnDefault = False
		End If
	Else
		blnDefault = False
	End If
	
	If IsMemberOfGroup(objUser, strGroup) = True Then
		On Error Resume Next
		objNetwork.AddWindowsPrinterConnection strPrinter
		If Err.Number <> 0 Then
			MsgBox "Error mapping printer " & strPrinter & ". Error " & Err.Number & ": " & Err.Description
			Err.Clear
		ElseIf blnDefault = True Then
			objNetwork.SetDefaultPrinter strPrinter
		End If
		On Error GoTo 0
	End If
Next

Function IsMemberOfGroup(objADUser, strGroupCN)
	Dim strGroupName
	If IsEmpty(objMemberships) = True Then
		Set objMemberships = CreateObject("Scripting.Dictionary")
		objMemberships.Add LCase("ALL"), 0
		If IsNull(objADUser.MemberOf) = False Then
			If TypeName(objADUser.MemberOf) = "String" Then
				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0
			Else
				For Each strGroupName In objADUser.MemberOf
					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0
				Next
			End If
		End If
	End If
	If objMemberships.Exists(LCase(strGroupCN)) = True Then
		IsMemberOfGroup = True
	Else
		IsMemberOfGroup = False
	End If
End Function

Open in new window

0
 
LVL 9

Expert Comment

by:abolinhas
ID: 34905294
Try this
Option Explicit
Dim objPrinter
Set objPrinter = CreateObject("WScript.Network") 
objPrinter.SetDefaultPrinter "\\ServerName\PrinterName"
' End of example VBScript

Open in new window

0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

Author Comment

by:GMH77
ID: 35092727
Sorry guys, havent been able to give you any update regarding this question.

I would like it to be as simple as possible is it possible to say somthing like
Map all printers here
Map printer 1
Map printer 2
and
If member of AD GRoupWithNamePrinter1
set default printer = \\SERVER\Printer1
and or
If member of AD GRoupWithNamePrinter2
set default printer = \\SERVER\Printer2
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35101515
OK, this will do that.

Regards,

Rob.
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY
' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP Function
Dim objMemberships

Set objNetwork = CreateObject("WScript.Network") 
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Section to map the network printers
objNetwork.AddWindowsPrinterConnection "\\server1\printer1"
objNetwork.AddWindowsPrinterConnection "\\server1\printer2"
objNetwork.AddWindowsPrinterConnection "\\server1\printer3"

If IsMemberOfGroup(objUser, "GroupWithNamePrinter1") = True Then
	On Error Resume Next
	objNetwork.SetDefaultPrinter "\\server\printer1"
	Err.Clear
	On Error GoTo 0
ElseIf IsMemberOfGroup(objUser, "GroupWithNamePrinter2") = True Then
	On Error Resume Next
	objNetwork.SetDefaultPrinter "\\server\printer2"
	Err.Clear
	On Error GoTo 0
ElseIf IsMemberOfGroup(objUser, "GroupWithNamePrinter3") = True Then
	On Error Resume Next
	objNetwork.SetDefaultPrinter "\\server\printer3"
	Err.Clear
	On Error GoTo 0
End If

Function IsMemberOfGroup(objADUser, strGroupCN)
	Dim strGroupName
	If IsEmpty(objMemberships) = True Then
		Set objMemberships = CreateObject("Scripting.Dictionary")
		objMemberships.Add LCase("ALL"), 0
		If IsNull(objADUser.MemberOf) = False Then
			If TypeName(objADUser.MemberOf) = "String" Then
				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0
			Else
				For Each strGroupName In objADUser.MemberOf
					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0
				Next
			End If
		End If
	End If
	If objMemberships.Exists(LCase(strGroupCN)) = True Then
		IsMemberOfGroup = True
	Else
		IsMemberOfGroup = False
	End If
End Function

Open in new window

0
 

Author Comment

by:GMH77
ID: 35252048
Sorry that I havent been able to answer back

I will try this configuration asap
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35720086
Hi, did you have any time to get back to this?

Regards,

Rob.
0
 

Author Comment

by:GMH77
ID: 35723215
No, havent got the time to test it cause customer wants to wait :(
Have planned to to this in June I hope
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question