'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Disable_Accounts.vbs ' ' Author Emmanuel Dreux ' email : edreux At ilinfo.fr ' http://www.ilinfo.fr/tools/Disable_Accounts.vbs.txt ' Last Modified: 2 Février 2009 ' ' Désactive les comptes passés en entrée, et les déplace dans OU quarantaine ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' History: ' ' Version 1.0 : Initial Release. Option Explicit ' Chemin du fichier csv Const strPathtoTextFile = "." Const strCsvFileName = "StaleAccounts.csv" ' Attributs de stockage Const StorePathAttribute = "adminDescription" Const StoreDateAttribute = "adminDisplayName" Const OUQUARANTINE = "OU=QUARANTINE" Const adOpenStatic = 3 Const adLockOptimistic = 3 Const adCmdText = &H0001 Const ADS_UF_ACCOUNTDISABLE = 2 Dim strDN, strFilter,strfqdn,strNetbiosDN Dim strQuery Dim oConnection 'As ADODB.Connection Dim oCmd 'As ADODB.Command Dim oRecordset 'As ADODB.Recordset Dim intReply Dim strFindUser Dim objUser Dim count Dim strsamAccountName, strDescription, strDescription2,strLine Dim strouquarantine, strtmpobj Dim oQuarantine Dim Flags Main Dim csvobjConnection Dim csvobjRecordSet Dim Today '---------------------------------------------------------------------------- ' Main '---------------------------------------------------------------------------- Sub Main() Today = Now() Set csvobjConnection = CreateObject("ADODB.Connection") Set csvobjRecordSet = CreateObject("ADODB.Recordset") csvobjConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & strPathToTextFile & ";" & _ "Extended Properties=""text;HDR=No;FMT=Delimited""" csvobjRecordset.Open "SELECT * FROM " & strCsvFileName, _ csvobjConnection, adOpenStatic, adLockOptimistic, adCmdText count = 0 ' 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 Do Until csvobjRecordset.EOF on error resume next strNetbiosDN = csvobjRecordset.Fields.Item("Domain") Select case LCASE(strNetbiosDN) case "mondomaine.local" strDN = "dc=mondomaine,dc=local" strfqdn = "mondomaine.local" case "child1.mondomaine.local" strDN = "dc=child1,dc=mondomaine,dc=local" strfqdn = "child1.mondomaine.local" case "child2.mondomaine.local" strDN = "dc=child2,dc=mondomaine,dc=local" strfqdn = "child2.mondomaine.local" end select strsamAccountName = csvobjRecordset.Fields.Item("samAccountName").Value if ( strsamAccountName = "") Then Elseif isNull(strsamAccountName ) Then Else strFilter = "(samAccountName=" & strsamAccountName & ")" on error resume next ' Use the filter strQuery = ";" & strFilter & ";Name,ADsPATH,description;subtree" strouquarantine = "LDAP://" & strfqdn & "/" & OUQUARANTINE & "," & strDN Set oQuarantine = GetObject(strouquarantine) If (Err.Number <> 0) Then Wscript.echo "Erreur initialisation: " & Err.Number End If ' Execute the query and loop through the result set oCmd.CommandText = strQuery Set oRecordset = oCmd.Execute do until oRecordset.EOF strDescription = "" strtmpobj = oRecordset.Fields("ADsPATH").Value set objUser = GetObject(strtmpobj) if Not IsNull(oRecordset.Fields("description").Value) Then For Each strLine in oRecordset.Fields("description").Value strDescription = strDescription & strLine Next End If objUser.Put StorePathAttribute, Cstr(strtmpobj) ' Store Date objUser.Put StoreDateAttribute, Cstr(Today) strDescription2 = Cstr(Today) & " ; " & Cstr(strtmpobj) & " ; " & strDescription objUser.Put "Description" , strDescription2 ' disable Flags = objUser.Get("userAccountControl") objUser.Put "userAccountControl", Flags xOR ADS_UF_ACCOUNTDISABLE ' Save objUser.SetInfo If (Err.Number <> 0) Then Wscript.echo "Erreur de sauvegarde sur cet objet: " & Err.Number End If ' Move to Quarantine oQuarantine.MoveHere strtmpobj, vbNullString If (Err.Number <> 0) Then Wscript.echo "Erreur de déplacement: " & Err.Number End If oRecordset.MoveNext exit do loop End if csvobjRecordset.MoveNext loop WScript.Quit(0) End Sub