Guest User

Export AD Users 2

a guest
Dec 5th, 2011
920
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. On Error Resume Next
  2. Const ADS_SCOPE_SUBTREE = 2
  3.  
  4. Const ADS_UF_ACCOUNTDISABLE = &H0002
  5. Const ADS_UF_PASSWD_NOTREQD = &H0020
  6. Const ADS_UF_PASSWD_CANT_CHANGE = &H0040
  7. Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
  8. Const ADS_UF_SMARTCARD_REQUIRED = &H40000
  9.  
  10.  
  11. 'Set RootDSE
  12. Set objRootDSE = GetObject("LDAP://rootDSE")
  13. strDomain = objRootDSE.Get("defaultNamingContext")
  14. strADPath = "LDAP://" & strDomain
  15. 'wscript.Echo strADPath
  16. Set objDomain = GetObject(strADPath)
  17. 'wscript.echo "objDomain: " & objDomain.distinguishedName
  18.  
  19.  
  20. Set objConnection = CreateObject("ADODB.Connection")
  21. Set objCommand =   CreateObject("ADODB.Command")
  22. objConnection.Provider = "ADsDSOObject"
  23. objConnection.Open "SAURON"
  24. Set objCommand.ActiveConnection = objConnection
  25.  
  26. objCommand.Properties("Page Size") = 1000
  27. objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  28.  
  29. objCommand.CommandText = _
  30.     "SELECT Name, mail, profilePath, homeDrive, distinguishedName,userAccountControl FROM '"& strADPath &"' WHERE objectCategory='user'"  
  31. Set objRecordSet = objCommand.Execute
  32.  
  33. objRecordSet.MoveFirst
  34. Set objFSO = CreateObject("scripting.filesystemobject")
  35. Set logStream = objFSO.opentextfile("C:\domainusers.csv", 8, True)
  36. logStream.writeline("Name,Email,Profile Path,Home Drive,Account Disabled,Password Required,User Changable Password,Password Expires,SmartCard Required,Login Count,Last Login,Last Password Change,Created,Modified")
  37. Do Until objRecordSet.EOF
  38.  
  39.     strDN = objRecordset.Fields("distinguishedName").Value
  40.     Set objUser = GetObject ("LDAP://" & strDN)
  41.      
  42.     If objRecordset.Fields("userAccountControl").Value AND ADS_UF_ACCOUNTDISABLE Then
  43.         Text = "Yes"
  44.     Else
  45.         Text = "No"
  46.     End If
  47.     If objRecordset.Fields("userAccountControl").Value AND ADS_UF_PASSWD_NOTREQD Then
  48.         Text = Text & ",No"
  49.     Else
  50.         Text = Text & ",Yes"
  51.     End If
  52.      
  53.     If objRecordset.Fields("userAccountControl").Value AND ADS_PASSWORD_CANT_CHANGE Then
  54.         Text = Text & ",No"
  55.     Else
  56.         Text = Text & ",Yes"
  57.     End If   
  58.     If objRecordset.Fields("userAccountControl").Value AND ADS_UF_DONT_EXPIRE_PASSWD Then
  59.         Text = Text & ",No"
  60.     Else
  61.         Text = Text & ",Yes"
  62.     End If
  63.     If objRecordset.Fields("userAccountControl").Value AND ADS_UF_SMARTCARD_REQUIRED Then
  64.         Text = Text & ",Yes"
  65.     Else
  66.         Text = Text & ",No"
  67.     End If
  68.    
  69.     logStream.writeline(objRecordset.Fields("Name").Value & ","_       
  70.         & objRecordset.Fields("mail").Value & ","_
  71.         & objRecordset.Fields("profilePath").Value & ","_
  72.         & objRecordset.Fields("homeDrive").Value & ","_
  73.         & text & ","_
  74.         & objUser.logonCount & ","_
  75.         & objUser.LastLogin & ","_
  76.         & objUser.PasswordLastChanged & ","_
  77.         & objUser.whenCreated & ","_
  78.         & objUser.whenChanged & ","_
  79.         )
  80.        
  81.     objRecordSet.MoveNext
  82. Loop
  83. logStream.Close
  84.  
RAW Paste Data