'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Liste_Comptes.vbs ' ' Author Emmanuel Dreux ' email : edreux At ilinfo.fr ' http://www.ilinfo.fr/tools/Liste_Comptes.vbs.txt ' Last Modified: 2 Février 2009 ' ' Liste les objets de l'AD. ' Affiche à l'écran et sauvegarde dans un fichier ' List objects in Active Directory ' Display to screen and Save to file ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' History: ' ' Version 1.0 : Initial Release. '---------------------------------------------------------------------------- ' Scripting constants '---------------------------------------------------------------------------- Option Explicit Const ADS_UF_ACCOUNTDISABLE = 2 Const ADS_GROUP_TYPE_GLOBAL_GROUP = &H00000002 Const ADS_GROUP_TYPE_LOCAL_GROUP = &H00000004 Const ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP = &H00000004 Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &H00000008 ' Path to Log File Const strLogFilePrefix = "ObjetsAD" Dim FSO Dim DateTimeStamp Dim strLogFile Dim oLogFile Dim logline Dim objHash ' Const Dictionnary Dim objgroupHash ' Const Dictionnary Dim oConnection 'As ADODB.Connection Dim oCmd 'As ADODB.Command Dim oRecordset 'As ADODB.Recordset Dim strQuery 'As String Dim strFilter 'As String Dim intTFD Dim key Dim cActiveAccounts 'Counter of objects found matching criteria Dim strDN Dim strfqdn Dim objDate Dim dtmDate Dim lngBias, lngBiasKey,lngHigh,lngLow Dim objShell Dim Environnement, strEnvironnement Dim ObjectTypes, strObjectTypes Dim strDescription, strLine Dim intgrouptype, strMemberOf,strgroupType Dim OuFilter, strouFilter Main '---------------------------------------------------------------------------- ' Main '---------------------------------------------------------------------------- Sub Main() ' Obtain local Time Zone bias from machine registry. Set objShell = CreateObject("Wscript.Shell") lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _ & "TimeZoneInformation\ActiveTimeBias") If (UCase(TypeName(lngBiasKey)) = "LONG") Then lngBias = lngBiasKey ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then lngBias = 0 For k = 0 To UBound(lngBiasKey) lngBias = lngBias + (lngBiasKey(k) * 256^k) Next End If Set objShell = Nothing ' RAZ des compteurs cActiveAccounts = 0 Environnement = InputBox("Choisissez l'environnement (ROOT,child1,child2)", "Choix Environnement","CHILD1") If Environnement = "" Then MsgBox("Aucune sélection d'environnement") WScript.Quit Else strEnvironnement = Environnement End If Select case LCASE(strEnvironnement) case "root" strDN = "dc=mondomaine,dc=local" strfqdn = "mondomaine.local" case "child1" strDN = "dc=child1,dc=mondomaine,dc=local" strfqdn = "child1.mondomaine.local" case "child2" strDN = "dc=child2,dc=mondomaine,dc=local" strfqdn = "child2.mondomaine.local" end select ObjectTypes = InputBox("Choisissez le type d'objets (group,User,Computer)", "Choix des objets","User") If ObjectTypes = "" Then MsgBox("Aucun objet sélectionné") WScript.Quit Else strObjectTypes = ObjectTypes End If Select case LCASE(strObjectTypes) case "group" strFilter = "(objectClass=group)" case "user" strFilter = "(&(ObjectCategory=Person)(objectClass=user))" case "computer" strFilter = "(objectClass=computer)" end select OuFilter = InputBox("Saisissez une OU en particulier, laissez vide pour tout le domaine", "Choix de l'OU","") if OuFilter ="" Then strouFilter="" Else strouFilter = OuFilter& "," End If ' Ouvre les fichiers de log Set FSO = CreateObject("scripting.filesystemobject") call GetDateTimeStamp() strLogFile = strLogFilePrefix + "_" + DateTimeStamp +".txt" Set oLogFile= FSO.CreateTextFile(strLogFile,TRUE) Set objHash = CreateObject("Scripting.Dictionary") objHash.Add "ADS_UF_ACCOUNTDISABLE", ADS_UF_ACCOUNTDISABLE Set objgroupHash = CreateObject("Scripting.Dictionary") objgroupHash.Add "GLOBAL_GROUP", ADS_GROUP_TYPE_GLOBAL_GROUP objgroupHash.Add "DOMAIN_LOCAL_GROUP", ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP objgroupHash.Add "UNIVERSAL_GROUP", ADS_GROUP_TYPE_UNIVERSAL_GROUP ' Set up the ADO Connection Set oConnection = CreateObject("ADODB.Connection") Set oCmd = CreateObject("ADODB.Command") oConnection.Provider = "ADSDsOObject" oConnection.Open "ADs Provider" oCmd.ActiveConnection = oConnection ' Set the page size to the default server limit oCmd.Properties("Page Size") = 1000 ' Use the filter and return the operating system information strQuery = ";" & strFilter & ";member,groupType,Description,samAccountName,name,userAccountControl,ADsPath,lastlogontimestamp;subtree" WScript.Echo strQuery & vbcrlf ' Execute the query and loop through the result set oCmd.CommandText = strQuery Set oRecordset = oCmd.Execute While Not oRecordset.EOF strDescription = "" logline = "" logline = oRecordset.Fields("name") & vbtab & oRecordset.Fields("samAccountName") ' Groupes if (LCASE(strObjectTypes) = "group") Then intgrouptype = oRecordset.Fields("groupType") For Each Key In objgroupHash.Keys If objgroupHash(Key) And intgrouptype Then strgroupType = Key end if next logline = logline & vbtab & strgroupType ' Vérifie si groupe vide strMemberOf = oRecordset.Fields("member").Value if ISNULL(strMemberOf) Then logline = logline & vbtab & "Empty" else logline = logline & vbtab & "" end if ' Users & Computers else intTFD = oRecordset.Fields("userAccountControl") On Error Resume Next Set objDate = oRecordset.Fields("lastLogonTimeStamp").Value If (Err.Number <> 0) Then On Error GoTo 0 dtmDate = #1/1/1601# Else On Error GoTo 0 lngHigh = objDate.HighPart lngLow = objDate.LowPart If (lngLow < 0) Then lngHigh = lngHigh + 1 End If If (lngHigh = 0) And (lngLow = 0 ) Then dtmDate = #1/1/1601# Else dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _ + lngLow)/600000000 - lngBias)/1440 End If End If For Each Key In objHash.Keys If objHash(Key) And intTFD Then logline = logline & vbtab & "Disabled" Else logline = logline & vbtab & "Enabled" end if next If (dtmDate = #1/1/1601#) Then logline = logline & vbtab & "Never" Else logline = logline & vbtab & dtmDate End If End if if Not IsNull(oRecordset.Fields("description").Value) Then For Each strLine in oRecordset.Fields("description").Value strDescription = strDescription & strLine Next end if logline = logline & vbtab & strDescription Call LogToFile(oLogFile,logline) oRecordset.MoveNext Wend WScript.Echo WScript.Echo WScript.Echo "Total Number of objects found: " & oRecordset.RecordCount WScript.Echo "Total Number of active objects: " & cActiveAccounts Set oRecordset = Nothing Set oConnection = Nothing WScript.Quit(0) End Sub '--------------------------------------------------------------------------------------------- ' Function LogToFile(ofile,strline) ' ' Log entry to file specified in input '--------------------------------------------------------------------------------------------- Function LogToFile(ofile, strline) ofile.WriteLine strline End Function '--------------------------------------------------------------------------------------------- ' Function: GetDateTimeStamp ' This function is responsible for getting the unique Date / Time stamp used for ' creating unique directory / file names. '--------------------------------------------------------------------------------------------- Function GetDateTimeStamp() On Error Resume Next Dim Seconds Dim Minutes Dim Hours Dim theDay Dim theMonth Hours = Hour(Now) Minutes = Minute(Now) Seconds = Second(Now) theDay = Day(Now) theMonth = Month(Now) If Len(Hours) = 1 Then Hours = "0" & Hours If Len(Minutes) = 1 Then Minutes = "0" & Minutes If Len(Seconds) = 1 Then Seconds = "0" & Seconds If Len(theDay) = 1 Then theDay = "0" & theDay If Len(theMonth) = 1 Then theMonth = "0" & theMonth DateTimeStamp = theMonth & "-" & theDay & "-" & Year(Now) & "_" & Hours & "-" & Minutes & "-" & Seconds End Function