Here you'll find a simple script which searches in Active Directory all objects that has a particular attribute (in this example : msExchHomeServerName)
'This VB Script searches an object in AD according to "SearchAttribute" (the attribute can be found using ADSIEDIT) 'The result is written on an Excel file (the file must be created befor excecuting the script) On Error Resume Next dim oRoot,oContainer,rs,strSearchUser,searchpath dim strDomainCtrl,strConnect,oConnect,oCommand Dim intRow,N N = 0 'number of the result intRow = 2 'used for the Excel file Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open _ ("C:\objectAD.xls") Set objWorksheet = objWorkbook.Worksheets(1) 'to get LDAP namespace set oRoot = GetObject("LDAP: //RootDSE") strDomainCtrl = oRoot.Get("defaultNamingContext") 'WScript.Echo (strDomainCtrl) strConnect = "LDAP://" & strDomainCtrl set oContainer = GetObject(strConnect) searchpath = oContainer.ADsPath 'use of ADO for Active Directory set oConnect = CreateObject("ADODB.Connection") set oCommand = CreateObject("ADODB.Command") 'opening the connection oConnect.Provider = "ADsDSOObject" oConnect.Open "Active Directory Provider" 'creating a command object for this connection Set oCommand.ActiveConnection = oConnect SearchAttribute = "/o=contosot/ou=First Administrative Group/cn=Configuration/cn=Servers/cn=SRVExchange" 'Search for all the objects that have msExchHomeServerName = "SearchAttribute" '(you can choose any attribute you want) 'and displays the corresponding "cn" in result oCommand.CommandText = "SELECT cn FROM '" & searchpath & "' WHERE msExchHomeServerName = '" & SearchAttribute & "'" 'creating a recordset based on the ADO command set rs = oCommand.Execute 'Navigating the record set If rs.EOF Then msgbox "No Result with this criteria" Else rs.MoveFirst While N <= rs.RecordCount WScript.Echo rs.Fields("Cn").Value objWorksheet.Cells(intRow,1).Value= rs.Fields("Cn").Value intRow = intRow + 1 N=N+1 rs.MoveNext wend End If objWorkbook.Save() oConnect.Close rs.Close |
Hope it helps.
Note: this script was not developed by me.
Imed.
No comments:
Post a Comment
Any suggestions ? comments ? or even errors found on this article..please let me know: