1

I have the following problem:

When i run my LOGON_SCRIPT.vbs to add network shares to users of a specific group it skips the first one if the user is part of Kleinkunstig_Users (it will not add the first share). But if i add another group it will add the share of the first one and the other. Please help? (script is below)

'start script

On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")

'find user name

strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)

'find user group's

For Each strGroup in objUser.MemberOf
strGroupPath = "LDAP://" & strGroup
Set objGroup = GetObject(strGroupPath)
strGroupName = objGroup.CN

' if user member of a group then map network drive

Select Case strGroupName
'
Case "Kleinkunstig_Users"
objNetwork.MapNetworkDrive "Y:", "\\KL01\Kleinkunstig"

Case "Kleinkunstig_Sales" 
objNetwork.MapNetworkDrive "V:", "\\KL01\Sales"

Case "Kleinkunstig_Marketing" 
objNetwork.MapNetworkDrive "M:", "\\KL01\Marketing"

Case "Kleinkunstig_Maanagement" 
objNetwork.MapNetworkDrive "X:", "\\KL01\Management"

Case "Kleinkunstig_IT" 
objNetwork.MapNetworkDrive "I:", "\\KL01\IT"

Case "Kleinkunstig_Financial" 
objNetwork.MapNetworkDrive "O:", "\\KL01\Financial"

Case "Kleinkunstig_Administrator" 
objNetwork.MapNetworkDrive "Q:", "\\KL01\Administrators"
'
End Select
Next

'end script
bk207
  • 173
  • 1
  • 2
  • 9

1 Answers1

1

In the following code, MemberOf may return either a collection or a string value (if the user is a member of only one group in addition to their primary group):

For Each strGroup in objUser.MemberOf

You can't "For Each" over objUser.MemberOf if objUser.MemberOf is a string.

There is probably an error which you are not seeing due to the following line:

On Error Resume Next

(note that removing that line from a script that many users are executing could mean that they all start receiving errors on login)

One very dirty way of testing and fixing this is to add the users in question to a "dummy" additional group. This should force the value returned by MemberOf to be a collection that you can then enumerate with For Each.

I have had success with a script similar to the one in this answer: Automatically run a script when I log on to Windows, quoted here:

Const ENGINEERING_GROUP     = "cn=engineering"
Const FINANCE_GROUP         = "cn=finance"
Const HUMAN_RESOURCES_GROUP = "cn=human resources"

Set wshNetwork = CreateObject("WScript.Network")
wshNetwork.MapNetworkDrive "h:",
"\\FileServer\Users\" & wshNetwork.UserName

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

If InStr(strGroups, ENGINEERING_GROUP) Then

    wshNetwork.MapNetworkDrive "g:",
    "\\FileServer\Engineering\"
    wshNetwork.AddWindowsPrinterConnection
    "\\PrintServer\EngLaser"
    wshNetwork.AddWindowsPrinterConnection
    "\\PrintServer\Plotter"
    wshNetWork.SetDefaultPrinter
    "\\PrintServer\EngLaser"

ElseIf InStr(strGroups, FINANCE_GROUP) Then

    wshNetwork.MapNetworkDrive "g:",
    "\\FileServer\Finance\"
    wshNetwork.AddWindowsPrinterConnection
    "\\PrintServer\FinLaser"
    wshNetWork.SetDefaultPrinter
    "\\PrintServer\FinLaser"

ElseIf InStr(strGroups, HUMAN_RESOURCES_GROUP) Then

    wshNetwork.MapNetworkDrive "g:",
    "\\FileServer\Human Resources\"
    wshNetwork.AddWindowsPrinterConnection
    "\\PrintServer\HrLaser"
    wshNetWork.SetDefaultPrinter
    "\\PrintServer\HrLaser"

End If

In theory, the above approach could cause issues if one of your group names is a substring of another, such as "finance" and "finance and auditing".

There are any number of other possible approaches, including testing the value returned by MemberOf to determine if it is a string or an array.

Possible alternative to using a login script to map network drives (may require more recent server / client versions than you currently have):

Best of luck!

jeff
  • 3,006
  • 1
  • 19
  • 10