修改AD用戶屬性(例如電話號碼、描述等)是AD管理員一項常見的基本工做,簡單卻十分繁瑣,特別是在對大批量用戶屬性進行修改的狀況下,工做量大且又是重複性的工做,若單個依次進行修改,效率低下,每每爲人們所抱怨。有沒有一種方便快捷、切能批量進行AD用戶屬性修改的方法呢?對此,本文做如下分享。(本文以批量修改AD用戶電話號碼屬性爲例來進行說明)ide
新建.txt文檔,輸入下述內容,並將此文檔另存爲chang.vbs文件。oop
Dim objInfo Const ADS_SCOPE_SUBTREE = 2 Set fso = CreateObject("Scripting.FileSystemObject") Set objFile = fso.OpenTextFile("Userinfo.csv") 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 Do On Error Resume Next objInfo = Split(objfile.ReadLine,",") strDN = FindUserDN(objInfo(0)) Set objUser = GetObject("LDAP://" & strDN) objUser.Put "mobile",objInfo(1) objUser.SetInfo If Err.Number <> 0 Then WScript.Echo "Error:" & objInfo(0) & "," & objInfo(1) End If strDN = "" Loop Until objfile.AtEndOfStream = True objfile.Close WScript.Echo "Done!" Function FindUserDN(strUserDisplayName) objCommand.CommandText = _ "SELECT distinguishedName FROM 'LDAP://OU=test,DC=contoso,DC=com' WHERE objectCategory='user' " & _ "AND displayname = '" & strUserDisplayName & "'" Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst Do Until objRecordSet.EOF arrDN = objRecordSet.Fields("distinguishedName").Value If IsArray(arrDN) = True Then WScript.Echo strUserDisplayName FindUserDN = arrDN(0) Else FindUserDN = arrDN End if objRecordSet.MoveNext Loop If Err.Number <> 0 Then WScript.Echo Err.mobile & ":" & strUserDisplayName Err.Clear End If End Function
建立名爲Userinfo.csv文件,文件內容根據實際須要,輸入顯示名稱和用戶的新電話號碼,以下圖所示:ui
將chang.vbs腳本和Userinfo.csv放到同一目錄,雙擊chang.vbs腳本。待chang.vbs腳本運行成功便可實現OU=temp,DC=contoso,DC=com下用戶IT00一、IT00二、IT00三、IT00四、IT005用戶號碼的更改,以下圖所示:ip
本文所述只是以修改AD用戶電話號碼爲例,若要實現不一樣OU下用戶其餘屬性的修改,可根據須要對此腳本作相應調整:文檔
一、修改用戶屬性項目,可對如下腳本內容進行調整:it
Set objUser = GetObject("LDAP://" & strDN) objUser.Put "mobile",objInfo(1) objUser.SetInfo
二、針對不一樣OU下的用戶可對如下腳本內容進行io
'LDAP://OU=test,DC=contoso, DC=com' WHERE objectCategory='user' " & _ "AND displayname = '" & strUserDisplayName & "'"