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

Map network drives and printers based on group

Status
Not open for further replies.

VAVS

IS-IT--Management
Mar 11, 2004
1
US
I have three distribution groups in AD for my three locations. I need to create a login script that will set two drives for each location, one fixed(S:=\\server\Company) and the other based on the user id (H:=\\server\users\userid)

I then need to map printers based on the group. Chicago will have 5 printers P1 - P5, Atlanta will have 2, P21,P22 and Memphis will have 1 P23

I have tried several scripts but get caught up when it comes to the group. Does the group have to be a security group or can it be a distribution group in AD?

I am attaching two files with scripts I have tried so far.
/code
ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
'Set objDomain = getObject("LDAP://rootDse")
DomainString = "AllianceSteelCorp.local"

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://AllianceSteelCorp.local/" & UserString) '2CONTACT IS THE DOMAIN

'Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName


'Synchronizes the time with Server our NTP Server
'WSHShell.Run "NET TIME \\server /set /y"

'Disconnect any drive mappings as needed.
'WSHNetwork.RemoveNetworkDrive "F:", True, True
'WSHNetwork.RemoveNetworkDrive "G:", True, True
'WSHNetwork.RemoveNetworkDrive "H:", True, True
'WSHNetwork.RemoveNetworkDrive "P:", True, True
'WSHNetwork.RemoveNetworkDrive "Q:", True, True
'WSHNetwork.RemoveNetworkDrive "V:", True, True
'WSHNetwork.RemoveNetworkDrive "U:", True, True
'WSHNetwork.RemoveNetworkDrive "X:", True, True
'WSHNetwork.RemoveNetworkDrive "Z:", True, True

'Disconnect ALL mapped drives
'Set clDrives = WshNetwork.EnumNetworkDrives
'For i = 0 to clDrives.Count -1 Step 2
' WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
'Next

'Give the PC time to do the disconnect, wait 300 milliseconds
'wscript.sleep 300

'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
WSHNetwork.MapNetworkDrive "X:", "\\server\users\" & UserString,True
WSHNetwork.MapNetworkDrive "W:", "\\server\Company",True
msgBox UserObj

'Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups
Select Case Lcase(GroupObj.Name)
'Check for group memberships and take needed action
'In this example below, Administrators and Presidents are groups.
Case "alliancechicago"
msgBox GroupObj.Name
msgBox UserString
WSHNetwork.AddWindowsPrinterConnection "\\server\P2", "P2"
WSHNetwork.AddWindowsPrinterConnection "\\server\P3", "P3"
WSHNetwork.AddWindowsPrinterConnection "\\server\P7", "P7"
WSHNetwork.AddWindowsPrinterConnection "\\server\P8", "P8"
WSHNetwork.AddWindowsPrinterConnection "\\server\P9", "P9"
WSHNetwork.AddWindowsPrinterConnection "\\server\P10", "P10"
WSHNetwork.AddWindowsPrinterConnection "\\server\PL", "PL"
WSHNetwork.AddWindowsPrinterConnection "\\server\PG", "PG"

Case "alliancememphis"
WSHNetwork.AddWindowsPrinterConnection "\\server\P23", "P23"
Case "allianceatlanta"
WSHNetwork.AddWindowsPrinterConnection "\\server\P21", "P21"

End Select
Next

'Remove ALL old printers
'Enumerate all printers first, after that you can select the printers you want by performing some string checks
'Set WSHPrinters = WSHNetwork.EnumPrinterConnections
'For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
End If
'To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above
'WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
'Next

'Remove a specific printer
'WSHNetwork.RemovePrinterConnection "\\ServerOld\HP5si",True,True

'Install Printers
'WSHNetwork.AddWindowsPrinterConnection "\\server\Canon iR2270/iR2870 PS3"
'WSHNetwork.AddWindowsPrinterConnection "\\server\BPS_102"

'Add On Code goes below this line
'=====================================

'Adding the Notepad Application to the SendTo Menu
'strSendToFolder = WSHShell.SpecialFolders("SendTo")
'strPathToNotepad = WinDir & "windows\system32\Notepad.exe"
'Set objShortcut = WSHShell.CreateShortcut(strSendToFolder & _
' "\Notepad.lnk")
'objShortcut.TargetPath = strPathToNotepad
'objShortcut.Save



'=====================================
'Add On Code goes above this line

'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing


'Quit the Script
wscript.quit
code/

/code
Set WSHNetwork = WScript.CreateObject("WScript.Network")
strUserID = WSHNetwork.UserName

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)




Set UserObj = GetObject("WinNT://AllianceSteelCorp.local/" & strUserID) '2CONTACT IS THE DOMAIN

'***Initialise Groups
'UserGroups="AllianceChicago"
'For Each GroupObj In UserObj.Groups
'UserGroups=UserGroups & "[" & GroupObj.Name & "]"

'Next

If MemberOf(ObjGroupDict, "AllianceChicago") Then
MsgBox "In the group!"
MsgBox strUserID
MsgBox strGroup
'If InGroup("AllianceChicago") Then
'MsgBox "In the group!"
'MsgBox strUserID
'MsgBox strGroup
Else
MsgBox "No no"
MsgBox strUserID
MsgBox strGroup

End If

'Function InGroup(strGroup)
'InGroup=False
' If InStr(UserGroups,"[" & strGroup & "]") Then
' InGroup=True
' End If
'End Function

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://" & strUserDomain & "/" & strUserName & ",user")

For Each objGroup In objUser.Groups

CreateMemberOfObject.Add objGroup.Name, "-"

Next

Set objUser = Nothing



End Function


code/
 
you have WHY too much code..

Code:
set objAD      = CreateObject("ADSystemInfo")
set objNetwork = WScript.CreateObject("Wscript.Network")
set objShell   = WScript.CreateObject("Wscript.Shell")

strComputer = "."
set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

'Remove Printers
set objPrinters = objNetwork.EnumPrinterConnections
for i = 1 to objPrinters.Count step 2
	objNetwork.RemovePrinterConnection objPrinters.item(i), true, true
next

'set printers based on group membership
set objUser = GetObject("LDAP://" & objAD.UserName)
for each objGroup in objUser.Groups
	select case lcase(objGroup.CN)
		case "group1" 
			objNetwork.AddWindowsPrinterConnection "\\server\Printer1"
		case "group2"
			objNetwork.AddWindowsPrinterConnection "\\server\Printer2a"
			objNetwork.AddWindowsPrinterConnection "\\server\Printer2b"
	end select
next

'oh, everyone get these drives
objNetwork.MapNetworkDrive "S:", "\\server\Company"
objNetwork.MapNetworkDrive "H:", "\\server\users\" & objAD.UserName

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top