Link to home
Start Free TrialLog in
Avatar of jlepre
jlepre

asked on

VBS Script to Map Printer Based on Group Membership

Hello,

I am trying to create a VBS Logon Script to map printers based on group membership.  I have two groups and two printers called PrinterA and PrinterB.  When I run the script I do not get any printer mappings or any error messages.  Any ideas on why this does not work?

Thanks in advance.
Const PrinterA ="cn=PrinterA"
Const PrinterB = "cn=PrinterB"
 
Set wshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))
 
If InStr(strGroups, PrinterA) Then
 
    wshNetwork.AddWindowsPrinterConnection "\\W2k8-INT\PrinterA"
    wshNetwork.AddWindowsPrinterConnection "\\W2k8-INT\PrinterB"
    wshNetWork.SetDefaultPrinter "\\W2k8-INT\PrinterA"
 
ElseIf InStr(strGroups, PrinterB) Then
 
    wshNetwork.AddWindowsPrinterConnection "\\W2k8-INT\PrinterB"
    wshNetwork.AddWindowsPrinterConnection "\\W2k8-INT\PrinterA"
    wshNetWork.SetDefaultPrinter "\\W2k8-INT\PrinterB"
 
End If

Open in new window

Avatar of tanelorn
tanelorn

Hi,

try this construct..

instead of mapping drives, put in your map network printer statements
this is just an example. but you should be able to modify it to your needs


On Error Resume Next
Dim WshNetwork, strUser, Domain, WshShell, Group
 
' Get the User ID
	Set WSHNetwork = WScript.CreateObject("WScript.Network")
		Domain = WSHNetwork.UserDomain   'nUser means data from Network Object
		strUser = ""
 
	While strUser = ""
		strUser = WSHNetwork.UserName
	Wend
 
'Call Main procedure to process the user groups information and map drives
	 call main()
		
 
'########## -------- End of logon script
'*************** sub procedures and Functions *******************************
 
Public Sub Main()
 
 
'-------------Main loop to detect groups that user belongs to
 
  Set objUser = GetObject("WinNT://" & Domain & "/" & strUser & ",user")
   For Each Group In objUser.groups
    Select Case Group.Name
		
 
	Case "ADC-RFC Users"
        	Call ADCRFCUsers()
 
        Case "ADC-Operator"
        	Call ADCOperator()
 
	case "ADC-CBUrestrict"
		call ADCOperator()
 
	case "ADC-CBUnorestrict"
		call ADCOperator()
 
	Case "ADC-Fab Restricted"
		Call ADCOperator()
 
        Case "ADC-Site Support"	
        	Call ADCITAdmin()
 
        Case "ADC-IT Admin"
        	Call ADCITAdmin()	
 
     End Select
   Next 'Group
 
   Set objUser = Nothing
 End If			
End Sub
 
'########################----drive mapping routines----######################
 
'****************************************************************************
Sub ADCRFCUsers()
 
  MapDrive "N:", "\\eadc-fs003\apps1"
  MapDrive "P:", "\\eadc-fs001\projects1"
  MapDrive "S:", "\\eadc-fs001\shared1"
  MapDrive "U:", "\\eadc-fs001\home1"
  MapDrive "w:", "\\eadc-fs003\world"
 
End Sub
'****************************************************************************
 
 
Sub ADCITAdmin()
	
  MapDrive "j:", "\\eadc-fs002\install"
  
 
End Sub
'****************************************************************************
 
sub ADCOperator()
 
  MapDrive "N:", "\\eadc-fs003\apps1"
  MapDrive "w:", "\\eadc-fs003\world"
 
End Sub
 
'****************************************************************************
Sub MapDrive(strDrive,strShare)
 
	On Error Resume Next
	WSHNetwork.MapNetworkDrive strDrive, strShare
 
	If Err.Number Then
 
		WSHNetwork.RemoveNetworkDrive strDrive
		WSHNetwork.MapNetworkDrive strDrive, strShare
 
	End If
		
End Sub
'#####################---- end drive mapping routines----####################
 
'
 
'End of SubProcedures and Functions

Open in new window

so you would have 2 case statements
case "PrinterA"       if that's the name of the group you want to map the A printer,
   call MapPrinterA()

case "PrinterB"
  call MapPrinterB()
and your

map printer subroutines will have the code to map the printers

ASKER CERTIFIED SOLUTION
Avatar of tanelorn
tanelorn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlepre

ASKER

Thanks for the help, I am in a training class today, I will give this a try later this afternoon.
Avatar of jlepre

ASKER

When I run the script I get the following error attached:
vbs-error.JPG
Avatar of jlepre

ASKER

I deleted the End If at Line 41 and all works well.  Thanks.