none
Need help with VB Login Script. Mapping drives by groups RRS feed

  • Question

  • Why does the below script only map the two drives based on group membership only when you are a member of BOTH groups? I want it to map the drive even if you only a member of one group.

    Option Explicit
    Dim objNetwork, objUser, CurrentUser
    Dim strGroup, strUserName

    on error resume next


    ' Initialise Groups with Const

    ' Const Acquisitions = "cn=Acquisitions"
     Const HMSScanners = "cn=HMSScanners"
     Const Terminated_Users = "cn=Terminated Users"

     

    ' Create objects and extract strGroup values

    Set objNetwork = CreateObject("WScript.Network")
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)

    strGroup = LCase(Join(CurrentUser.MemberOf))
    strUserName = objNetwork.UserName


    ' Remove old drive mapings

    objNetwork.RemoveNetworkDrive "P:", True, True
    objNetwork.RemoveNetworkDrive "G:", True, True
    objNetwork.RemoveNetworkDrive "Z:", True, True
    objNetwork.RemoveNetworkDrive "S:", True, True
    objNetwork.RemoveNetworkDrive "T:", True, True
    objNetwork.RemoveNetworkDrive "F:", True, True

    ' Map drives for all users

    objNetwork.MapNetworkDrive "Z:" , "\\HOSHFILE1.HOSH.LOCAL\USERS$" & "\" & strUserName, TRUE
    objNetwork.MapNetworkDrive "P:" , "\\HOSHFILE1.HOSH.LOCAL\PoliciesandProcedures", TRUE
    objNetwork.MapNetworkDrive "S:" , "\\HOSHFILE1.HOSH.LOCAL\Allshare", TRUE
    objNetwork.MapNetworkDrive "G:" , "\\HOSHFILE1.HOSH.LOCAL\Groups", TRUE


    ' Tests for membership in the Terminated Users Group and maps drive for members

     If InStr(strGroup, lcase(Terminated_Users)) Then
     objNetwork.MapNetworkDrive "T:", "\\HOSHFILE1.HOSH.LOCAL\TERMINATEDUSERS$", TRUE
     End If


    ' Tests for membership in the HMSScanners Users Group and maps drive for members

     If InStr(strGroup, lcase(HMSScanners)) Then
     objNetwork.MapNetworkDrive "F:", "\\10.15.14.100\root", TRUE, "network", "network"
     End If

     

    Tuesday, October 5, 2010 2:51 AM

Answers

  • I would suggest the following:

    Option Explicit
    Dim objNetwork, objUser, CurrentUser
    Dim strGroup, strUserName
    Dim arrGroups
    
    ' Initialise Groups with Const
    ' Const Acquisitions = "cn=Acquisitions"
    Const HMSScanners = "cn=HMSScanners"
    Const Terminated_Users = "cn=Terminated Users"
    
    ' Create objects and extract strGroup values
    Set objNetwork = CreateObject("WScript.Network")
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
    
    On Error Resume Next
    arrGroups = CurrentUser.GetEx("memberOf")
    If (Err.Number <> 0) Then
      On Error GoTo 0
      strGroups = ""
    Else
      On Error GoTo 0
      strGroups = LCase(Join(arrGroups))
    End If 
    
    strUserName = objNetwork.UserName
    
    ' Remove old drive mapings
    On Error Resume Next
    objNetwork.RemoveNetworkDrive "P:", True, True
    objNetwork.RemoveNetworkDrive "G:", True, True
    objNetwork.RemoveNetworkDrive "Z:", True, True
    objNetwork.RemoveNetworkDrive "S:", True, True
    objNetwork.RemoveNetworkDrive "T:", True, True
    objNetwork.RemoveNetworkDrive "F:", True, True
    On Error GoTo 0
    
    ' Map drives for all users
    objNetwork.MapNetworkDrive "Z:" , "\\HOSHFILE1.HOSH.LOCAL\USERS$" & "\" & strUserName, TRUE
    objNetwork.MapNetworkDrive "P:" , "\\HOSHFILE1.HOSH.LOCAL\PoliciesandProcedures", TRUE
    objNetwork.MapNetworkDrive "S:" , "\\HOSHFILE1.HOSH.LOCAL\Allshare", TRUE
    objNetwork.MapNetworkDrive "G:" , "\\HOSHFILE1.HOSH.LOCAL\Groups", TRUE
    
    ' Tests for membership in the Terminated Users Group and maps drive for members
    If InStr(strGroup, lcase(Terminated_Users)) Then
      objNetwork.MapNetworkDrive "T:", "\\HOSHFILE1.HOSH.LOCAL\TERMINATEDUSERS$", TRUE
    End If
    
    ' Tests for membership in the HMSScanners Users Group and maps drive for members
    If InStr(strGroup, lcase(HMSScanners)) Then
      objNetwork.MapNetworkDrive "F:", "\\10.15.14.100\root", TRUE, "network", "network"
    End If
    

     

    I see I referred to objCurrent in my earlier snippet, when it should have been CurrentUser. Again, I only use "On Error Resume Next" where I expect errors. I've never used IP addresses when I map drives, but I assume you have tested and it works for you.

    Richard Mueller


    MVP ADSI
    Tuesday, October 5, 2010 2:23 PM
    Moderator

All replies

  • This statement:

    strGroup = LCase(Join(CurrentUser.MemberOf))

    will raise an error a type mismatch error unless the memberOf attribute has at least two distinguished names (DN's), and it never includes the "primary" group (usually "Domain Users"). That is because the Join function requires an array. The best solution is:

    On Error Resume Next
    arrGroups = objCurrent.GetEx("memberOf")
    If (Err.Number <> 0) Then
      On Error GoTo 0
      strGroups = ""
    Else
      On Error GoTo 0
      strGroups = LCase(Join(arrGroups))
    End If 
    

     

    The GetEx function still results in an error if memberOf has not DN's, but if there is just one it retieves the value an array, so the Join function does not raise an error. You still must trap the possible error if there are no DN's. Notice also that I use "On Error Resume Next" only where necessary.

    Richard Mueller


    MVP ADSI
    Tuesday, October 5, 2010 12:02 PM
    Moderator
  • Where exactly and do I add that? Sorry I am a nOOB to VBScript.
    Tuesday, October 5, 2010 1:54 PM
  • I would suggest the following:

    Option Explicit
    Dim objNetwork, objUser, CurrentUser
    Dim strGroup, strUserName
    Dim arrGroups
    
    ' Initialise Groups with Const
    ' Const Acquisitions = "cn=Acquisitions"
    Const HMSScanners = "cn=HMSScanners"
    Const Terminated_Users = "cn=Terminated Users"
    
    ' Create objects and extract strGroup values
    Set objNetwork = CreateObject("WScript.Network")
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
    
    On Error Resume Next
    arrGroups = CurrentUser.GetEx("memberOf")
    If (Err.Number <> 0) Then
      On Error GoTo 0
      strGroups = ""
    Else
      On Error GoTo 0
      strGroups = LCase(Join(arrGroups))
    End If 
    
    strUserName = objNetwork.UserName
    
    ' Remove old drive mapings
    On Error Resume Next
    objNetwork.RemoveNetworkDrive "P:", True, True
    objNetwork.RemoveNetworkDrive "G:", True, True
    objNetwork.RemoveNetworkDrive "Z:", True, True
    objNetwork.RemoveNetworkDrive "S:", True, True
    objNetwork.RemoveNetworkDrive "T:", True, True
    objNetwork.RemoveNetworkDrive "F:", True, True
    On Error GoTo 0
    
    ' Map drives for all users
    objNetwork.MapNetworkDrive "Z:" , "\\HOSHFILE1.HOSH.LOCAL\USERS$" & "\" & strUserName, TRUE
    objNetwork.MapNetworkDrive "P:" , "\\HOSHFILE1.HOSH.LOCAL\PoliciesandProcedures", TRUE
    objNetwork.MapNetworkDrive "S:" , "\\HOSHFILE1.HOSH.LOCAL\Allshare", TRUE
    objNetwork.MapNetworkDrive "G:" , "\\HOSHFILE1.HOSH.LOCAL\Groups", TRUE
    
    ' Tests for membership in the Terminated Users Group and maps drive for members
    If InStr(strGroup, lcase(Terminated_Users)) Then
      objNetwork.MapNetworkDrive "T:", "\\HOSHFILE1.HOSH.LOCAL\TERMINATEDUSERS$", TRUE
    End If
    
    ' Tests for membership in the HMSScanners Users Group and maps drive for members
    If InStr(strGroup, lcase(HMSScanners)) Then
      objNetwork.MapNetworkDrive "F:", "\\10.15.14.100\root", TRUE, "network", "network"
    End If
    

     

    I see I referred to objCurrent in my earlier snippet, when it should have been CurrentUser. Again, I only use "On Error Resume Next" where I expect errors. I've never used IP addresses when I map drives, but I assume you have tested and it works for you.

    Richard Mueller


    MVP ADSI
    Tuesday, October 5, 2010 2:23 PM
    Moderator
  • Worked like a champ when I fixed a couple of spelling errors and removed the On Error Go To 0 after the RemoveMap commands.

     

    The IP I am mapping to is a IBM Mainframe.

     

    Thanks.

    Wednesday, October 6, 2010 1:08 AM