Tuesday, 27 August 2013

Query disabled user in Active Directory

Some times we need to use programing to generate something more faster, especially if you handling more than 100 records of data.

This time i need to query user that are disabled in Active Directory. Sure you can see it by search in Active Directory control pane, but i need it to be exported in csv so i can handle it in excell format for futher processing.

So here are the script to know who is disabled in Active directory and save it to a csv file.


Const ADS_UF_ACCOUNTDISABLE = 2 
  
Set objConnection = CreateObject("ADODB.Connection") 
objConnection.Open "Provider=ADsDSOObject;" 
Set objCommand = CreateObject("ADODB.Command") 
objCommand.ActiveConnection = objConnection 
objCommand.CommandText = _ 
    ";(objectCategory=User)" & _ 
        ";userAccountControl,distinguishedName;subtree"   
Set objRecordSet = objCommand.Execute 
  
intCounter = 0 
Do Until objRecordset.EOF 
    intUAC=objRecordset.Fields("userAccountControl") 
    If intUAC AND ADS_UF_ACCOUNTDISABLE Then 
        content = objRecordset.Fields("distinguishedName") & ",disabled" 
        writeToFile(content)
        intCounter = intCounter + 1 
    End If 
    objRecordset.MoveNext 
Loop 
  
WScript.Echo VbCrLf & "A total of " & intCounter & " accounts are disabled." 
  
objConnection.Close 

Function writeToFile(content)
    Const ForAppending = 8 'for logging
    Dim objFSO, objLogFile 'for logging
    fname = "c:\users\masterUser\documents\disabledUser.csv"
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objLogFile = objFSO.OpenTextFile(fname, ForAppending, True)
 objLogfile.WriteLine content
 objLogFile.Close
 set objLogFile = Nothing
 set objFSO = Nothing
    writeToFile = "success"
End Function

So you should expect your file in c:\users\masterUser\documents\disabledUser.csv

Hope this help someone like me.

 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More