登录VBS以通过OU和组映射驱动器

时间:2014-04-21 19:03:38

标签: authentication vbscript mapping drive-mapping

我试图通过调用从包含它们的OU中查找用户的子例程来使以下登录脚本工作。然后根据它们所在的组在子例程中完成驱动器映射。我想避免创建组,只使用每个OU的域用户,而不是必须为这么大的公司中的每个OU维护它们,因此在第一级上按组过滤OU然后更容易。我不能让这个工作,但任何帮助将不胜感激。我觉得我很亲密......

On Error Resume Next
Dim objfileserverO,objFILE,objShell,objNetwork
set objfileserverO=CreateObject("Scripting.FileSystemObject")
set objShell=CreateObject("Wscript.Shell")
set objNetwork=CreateObject("Wscript.Network")
set objSysInfo = CreateObject("ADSystemInfo")



strUserDN = objSysInfo.UserName

Set objUser = GetObject("LDAP://" & strUserDN)

strOUPath = objUser.Parent

Function GetUserOU(strOUPath)
Select Case strOUPath
Case "LDAP://ou=Users,ou=abzed,ou=domain,dc=domain,dc=org"
    Call MapIt1
Case "LDAP://ou=Users,ou=huston,ou=domain,dc=domain,dc=org"
    Call MapIt2
Case "LDAP://ou=Users,ou=denv,ou=IEB,ou=domain,dc=domain,dc=org"
    Call MapIt3
Case "LDAP://ou=Users,ou=huston,ou=IEB,ou=domain,dc=domain,dc=org"
    Call MapIt4
Case "LDAP://ou=Users,ou=lafa,ou=IEB,ou=domain,dc=domain,dc=org"
    Call MapIt5
Case "LDAP://ou=Users,ou=NEW,ou=IEB,ou=domain,dc=domain,dc=org"
    Call MapIt6
End Select
End Function

Function IsAMemberOf(strDomain,strUser,strGroup)
On Error Resume Next
Set objUser=GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
Set objGrp=GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")

If objGrp.IsMember(objUser.ADsPath) Then
IsAMemberOf=True
Else
IsAMemberOf=False
End If 

End Function


Sub MapIt1(strDrive,strMap)
On Error Resume Next
If objfileserverO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"OG_SystemsIntegration") Then     MapIt1 "K:","\\domain-fileserver2.domaingroup.com\Shared\SI"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"OG_BidsPropsals") Then MapIt1 "L:","\\domain-fileserver2.domaingroup.com\Shared\BIDS"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"OG_BidsGuest") Then MapIt1 "L:","\\domain-fileserver2.domaingroup.com\Shared\BIDS"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_SALES") Then MapIt1 "N:","\\domain-fileserver2.domaingroup.com\shared\sales"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"OG_Operations") Then MapIt1 "O:","\\domain-fileserver2.domaingroup.com\shared\Operations"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_Stores") Then MapIt1 "Q:","\\domain-fileserver2.domaingroup.com\shared\stores"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_Accounts") Then MapIt1 "S:","\\domain-fileserver2.domaingroup.com\shared\accounts"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_IT") Then MapIt1 "T:","\\domain-fileserver2.domaingroup.com\shared\IT"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_MIS") Then MapIt1 "V:","\\domain-fileserver2.domaingroup.com\shared\MIS"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_Archive") Then MapIt1 "W:","\\domain-fileserver2.domaingroup.com\Archive"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_INTRANET") Then MapIt1 "X:","\\domain-fileserver2.domaingroup.com\shared\intranet"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"SUP_BUSINESS") Then MapIt1 "Y:","\\domain-fileserver2.domaingroup.com\shared\Business"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt1 "P:","\\domain-fileserver2.domaingroup.com\products"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt1 "U:","\\domain-fileserver2.domaingroup.com\companyinfo"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt1 "Z:","\\domain-fileserver2.domaingroup.com\Shared\scratchpad"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"UK Engineers") Then MapIt1 "I:","\\domain.Org\shared"
End If
End Sub


Sub MapIt2(strDrive,strMap)
On Error Resume Next
If objfileserverO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain User") Then MapIt2    "I:","\\domain.Org\shared"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"domain Leadership Team_Agings") Then MapIt2 "M:","\\domain.org\shared\Leadership Team"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"domain International Tax") Then MapIt2 "L:","\\domain.org\shared\Tax\"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"domain_NO") Then MapIt2 "V:","\\fileserver01\visma"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"abzedserver") Then MapIt2 "Q:","\\server1\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Site server5 Groups") Then MapIt2 "R:","\\server1\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"abzedserver") Then MapIt2 "S:","\\server\"
End If
End Sub


Sub MapIt3(strDrive,strMap)
On Error Resume Next
If objfileserverO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt3 "G:","\\server2\apps"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt3 "P:","\\server2\public"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt3 "U:","\\server2\users"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt3 "X:","\\server2\groups"
End If
End Sub


Sub MapIt4(strDrive,strMap)
On Error Resume Next
If objfileserverO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt4 "X:","\\server5"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt4 "L:","\\server4\applications"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Group 3 - r") Then MapIt4 "Y:","\\server3\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Group 3 - rw") Then MapIt4 "Y:","\\server3\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"System Maintenance - r") Then MapIt4 "Z:","\\server3\groups\System Maintenance"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"System Maintenance - rw") Then MapIt4 "Z:","\\server3\groups\System Maintenance"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Marketing") Then MapIt4 "I:","\\domain.org\shared"
End If
End Sub


Sub MapIt5(strDrive,strMap)
On Error Resume Next
If objfileserverO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt5 "I:","\\fileserver1\Sites"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Procurement and Logistics") Then MapIt "S:","\\domain.org\shared"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt5 "V:","\\fileserver1\PRIV_"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt5 "L:","\\fileserver1\applications"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt5 "P:","\\campappfileserver1\StvPFILE"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt5 "H:","\\fileserver1\rti4"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Tier III - r") Then MapIt5 "Y:","\\server3\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Tier III - rw") Then MapIt5 "Y:","\\server3\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"System Maintenance - r") Then MapIt5 "Z:","\\server3\groups\System Maintenance"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"System Maintenance - rw") Then MapIt5 "Z:","\\server3\groups\System Maintenance"
End If
End Sub


Sub MapIt6(strDrive,strMap)
On Error Resume Next
If objfileserverO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt6 "Y:","\\server3\groups"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt6 "M:","\\server3\users"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt6 "X:","\\server3\apps"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt6 "L:","\\fileserver1\applications"
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"Domain Users") Then MapIt6 "Z:","\\server3\groups\System Maintenance"
End If
End Sub

1 个答案:

答案 0 :(得分:0)

我最终废弃了这个长时间过于多风的脚本,并在子程序中更容易重复,尽管我真的希望上述脚本能够工作。

Set objSysInfo = CreateObject("ADSystemInfo")


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

strUserName = objUser.samAccountName

strOUPath = objUser.Parent
arrContainers = Split(strOUPath, ",")
arrOU = Split(arrContainers(0), "=")
strOU = arrOU(1)

If arrOU(1) = "OU=test2,OU=test1,OU=TestOU,DC=domain,DC=com" Then
strDrive = "\\sharename\share\share"
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "Z:", strDrive
End If