Search This Blog

Thursday, April 22, 2010

Login script : Map network drive VBS script

Using VBS script to map network drive allows you to map different users from different groups to different paths using a single script.

Pros:
1. Less maintenance required, in case of modification, only 1 script needs to be modified.
2. Centralized control. 1 single GPO.

Cons:
1. Extra scripting needs to be done for special users.
2. Not as granular as having different login script for different groups.

WshNetwork.MapNetworkDrive
Creating Login Scripts

Here is an example of my modified script.

=====================================
' Default drive letter for User and Department Shared Folder
Const User_drive = "H:"
Const Dept_drive = "G:"


' Define User Location
' E.g. For user in TPM, they should be group under "DLMYO-TPM All Staff"
Const TPM = "CN=DLMYO-TPM All Staff"


' Define Security Groups property here.
' E.g. for IA, Group name is "DLMYO-IA-SERVER".
Const BCP = "cn=DLMYO-BCP"
Const TSS = "cn=DLMYO-TSS"
Const IA = "cn=DLMYO-IA-SERVER"
Const EMC = "cn=DLMYO-EMC"
Const DCM = "cn=DLMYO-DCM"




Dim wshDrives, i


Set wshNetwork = CreateObject("WScript.Network")

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))



' Loop through the network drive connections and disconnect any that match User_drive and Dept_Drive
Set wshDrives = wshNetwork.EnumNetworkDrives
If wshDrives.Count > 0 Then
For i = 0 To wshDrives.Count-1 Step 1
If wshDrives.Item(i) = User_Drive Then
wshNetwork.RemoveNetworkDrive User_drive, True, True
Elseif wshDrives.Item(i) = Dept_drive Then
wshNetwork.RemoveNetworkDrive Dept_drive, True, True
End If
Next
End If






' Map User folder depending on user's location
If InStr(1, strGroups, TPM, 1) Then

wshNetwork.MapNetworkDrive User_drive, "\\aosrv008\Users\" & wshNetwork.UserName, TRUE

End If




' Map department shared folder depending on user's security group
If InStr(1, strGroups, BCP, 1) Then

wshNetwork.MapNetworkDrive Dept_drive, "\\aosrv008\BCP"

ElseIf InStr(1, strGroups, TSS, 1) Then

wshNetwork.MapNetworkDrive Dept_drive, "\\aosrv008\TSS"

ElseIf InStr(1, strGroups, IA, 1) Then

wshNetwork.MapNetworkDrive Dept_drive, "\\aosrv008\IA"

ElseIf InStr(1, strGroups, EMC, 1) Then

wshNetwork.MapNetworkDrive Dept_drive, "\\aosrv008\EMC"

ElseIf InStr(1, strGroups, DCM, 1) Then

wshNetwork.MapNetworkDrive Dept_drive, "\\aosrv008\DCM"

End If

wscript.echo "Departmental shared folder G: and User personal folder H: were mapped."
=====================================

No comments:

Post a Comment