您的位置:首页 > 理论基础

活动目录管理---批量用户添加登录到指定计算机

2012-11-14 14:24 417 查看
需求:公司采用域环境管理,所有用户都绑定到只能登录到某一台电脑使用,只在会议室增加一台公用电脑,为了方便资源访问,让所有用户都可以登录这台电脑使用,如果一个一个用户这样添加登录到指定电脑,是可以实现的,但工作量相当的大,因为有用户好几百,或几K的时候就成问题了。

以下是通过参考微软官方技术支持文档改写的批处理,快速实现批量用户添加绑定到相应电脑登录。

运行环境:需在域服务器上以域管理员身份运行以下vbs批处理

1、批量添加,如把ou为:二级---行政部 里所有用户都开通可以使用计算机名为testpc的电脑

strNewComputer = "testpc"

Const ADS_SCOPE_SUBTREE = 2

Const ADS_PROPERTY_UPDATE = 2

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _

"SELECT distinguishedName,userWorkstations FROM 'LDAP://ou=行政部,ou=二级,dc=domain,dc=com' WHERE objectCategory='user'"

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

strUserWS = objRecordSet.Fields("userWorkstations").Value

strDN = objRecordSet.Fields("distinguishedName").Value

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

If strUserWS <> "" And InStr(strUserWS,strNewComputer) = 0 Then

strTemp = strUserWS & "," & strNewComputer

objUser.Put "userWorkstations",strTemp

objUser.SetInfo

End If

objRecordSet.MoveNext

Loop

把上述脚本保存为add.vbs ,以域管理员权限在域服务器上执行即可。

2、批量删除,如撤销上述操作:

strNewComputer = "testpc"

Const ADS_SCOPE_SUBTREE = 2

Const ADS_PROPERTY_UPDATE = 2

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _

"SELECT distinguishedName,userWorkstations FROM 'LDAP://ou=行政部,ou=二级,dc=domain,dc=com' WHERE objectCategory='user'"

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

strUserWS = objRecordSet.Fields("userWorkstations").Value

strDN = objRecordSet.Fields("distinguishedName").Value

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

If InStr(strUserWS,strNewComputer) <> 0 Then

strTemp = Replace(strUserWS,"," & strNewComputer,"")

objUser.Put "userWorkstations",strTemp

objUser.SetInfo

End If

objRecordSet.MoveNext

Loop

把上述脚本保存为delete.vbs ,以域管理员权限在域服务器上执行即可。

注:1、红色字体为根据用户实际环境修改;

2、上述操作不影响原来已经添加有的计算机;

3、由于实际环境的差异,请测试后再在生产环境下使用!

本文出自 “MG05 學習筆記~” 博客,请务必保留此出处http://pimg2005.blog.51cto.com/842469/1059737
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: