如何在 Active Directory 中枚舉計算機帳號
使用以下腳本進行計算機帳號的枚舉。
' ListAllComputerInfo.vbs
' 在 Active Directory 中枚舉計算機帳號
' 陳濤 2008-11-22 12:12:06
' http://msdn.microsoft.com/en-us/library/ms675090(VS.85).aspx
' 根據您的需要修改以下參數
' 如果整個域 strSearchScope="LDAP://DC=zyy,DC=local"
' 如果某個OU strSearchScope="LDAP://OU=辦公室,DC=zyy,DC=local"
'strSearchScope="LDAP://OU=辦公室,DC=zyy,DC=local"
strSearchScope="LDAP://DC=zyy,DC=local"
' 輸出文件名
strOutPutFile="c:\ADComuterInfo.csv"
' 文件操作
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(strOutPutFile, ForAppending, True)
' 取得域中計算機的信息
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select name,adsPath,accountExpires,whenCreated, whenChanged from '" & strSearchScope & "' " _
& "where objectClass='user'"
'objCommand.CommandText = _
' "Select AdsPath from '" & strSearchScope & "' " _
' & "where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
i=0
objTextFile.WriteLine("")
'objTextFile.WriteLine("開始時間:" & now())
objTextFile.WriteLine "name,sAMAccountName,displayname,description,mail,accountExpires"
Do Until objRecordSet.EOF
i=i+1
objTextFile.Write(objRecordSet.Fields("Name").Value & ",")
'Set objDate = objRecordSet.accountExpires
'lngDate = (objDate.HighPart * (2^32)) * objDate.LowPart
'objTextFile.Write(lngDate & ",")
Set objUser = GetObject(objRecordSet.Fields("AdsPath").Value)
objTextFile.Write( objUser.AccountExpirationDate & ",")
Set objUser = Nothing
objTextFile.Write(objRecordSet.Fields("whenCreated").Value & ",")
objTextFile.Write(objRecordSet.Fields("whenChanged").Value & ",")
objTextFile.WriteLine("")
objRecordSet.MoveNext
Loop
'objTextFile.WriteLine("結束時間:" & Now())
objTextFile.Close
Wscript.Echo "已成功地將 " & i & "條信息寫入到 " & strOutPutFile & "中。"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.