我有以下问题:
当我运行 LOGON_SCRIPT.vbs 为特定组的用户添加网络共享时,如果用户属于 Kleinkunstig_Users,它会跳过第一个共享(它不会添加第一个共享)。但如果我添加另一个组,它会添加第一个和另一个组的共享。请帮忙?(脚本如下)
'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
答案1
在下面的代码中,MemberOf 可能返回一个集合或一个字符串值(如果用户除了其主要组之外还只是一个组的成员):
For Each strGroup in objUser.MemberOf
如果 objUser.MemberOf 是字符串,则不能对 objUser.MemberOf 进行“For Each”操作。
可能存在一个您没有看到的错误,原因是以下行:
On Error Resume Next
(请注意,从许多用户正在执行的脚本中删除该行可能意味着他们都会在登录时开始收到错误)
测试和修复此问题的一种非常不雅的方法是将相关用户添加到“虚拟”附加组中。这应该会强制 MemberOf 返回的值成为一个集合,然后您可以使用 For Each 进行枚举。
我已经成功使用了类似于这个答案中的脚本:当我登录 Windows 时自动运行脚本,引用如下:
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
理论上,如果您的一个组名是另一个组名的子字符串,例如“finance”和“finance and auditing”,则上述方法可能会导致问题。
还有许多其他可能的方法,包括测试 MemberOf 返回的值以确定它是字符串还是数组。
使用登录脚本映射网络驱动器的可能替代方法(可能需要比当前更新的服务器/客户端版本):
祝你好运!