WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Traverse OU and Generate a Report of Objects

 Keywords:  ADSI AD OU Objects

;***********************************************************************************************
;**                                                                                           **
;**   Use this script to traverse OU folders and generate a report of objects in those OUs.   **
;**                                                                                           **
;***********************************************************************************************

AddExtender("WWADS44I.DLL")
Home = DirHome()
Domain=RegQueryValue(@REGMACHINE,"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters[Domain]")
If Domain == 0 Then Domain = "Company.Corp"
Company = StrLower(ItemExtract(1,Domain,"."))
sChildren = StrCat(`LDAP://`,Company)
KeepGoing = @TRUE
OutputFileName = StrCat(Home,`Output.log`)
Parentpath = sChildren

While KeepGoing
   Menu = `GetChild`
   ErrorMode(@OFF)
   Parentpath = dsGetPrntPath(sChildren)
   Current_Path = sChildren
   sUsers = dsGetChldPath(sChildren, "user")
   sUsers_Display = StrReplace(sUsers,@TAB,@CRLF)
   sChildren = dsGetChldPath(sChildren, "Container   OrganizationalUnit")
   DefaultChild = ItemExtract(1,sChildren,@TAB)
   ErrorMode(@CANCEL)
   MyDialogFormat=`WWWDLGED,6.1`
   MyDialogCaption=`Currently displaying all users in "%Current_Path%"`
   MyDialogX=019
   MyDialogY=076
   MyDialogWidth=374
   MyDialogHeight=117
   MyDialogNumControls=006
   MyDialogProcedure=`DEFAULT`
   MyDialogFont=`DEFAULT`
   MyDialogTextColor=`DEFAULT`
   MyDialogBackground=`DEFAULT,DEFAULT`
   MyDialogConfig=0
   MyDialog001=`051,099,042,012,PUSHBUTTON,DEFAULT,"Next level >",1,5,32,DEFAULT,DEFAULT,DEFAULT`
   MyDialog002=`317,099,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
   MyDialog003=`003,005,364,108,DROPLISTBOX,sChildren,"%DefaultChild%",DEFAULT,1,4,DEFAULT,DEFAULT,DEFAULT`
   MyDialog004=`125,099,132,012,PUSHBUTTON,DEFAULT,"Create Report of all users displayed above",2,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
   MyDialog005=`005,099,042,012,PUSHBUTTON,DEFAULT,"< Previous level",3,4,0,DEFAULT,DEFAULT,DEFAULT`
   MyDialog006=`005,021,358,072,MULTILINEBOX,sUsers_Display,DEFAULT,DEFAULT,10,8,DEFAULT,DEFAULT,DEFAULT`
   ButtonPushed=Dialog("MyDialog")
   Switch ButtonPushed
      Case 1
         Break
      Case 2
         KeepGoing = @FALSE
         Break
      Case 3
         sChildren = Parentpath
         Break
   EndSwitch
EndWhile

OutputFile = FileOpen(OutputFileName, "Write")
   FileWrite(OutputFile, StrCat(`All user accounts in `,Current_Path))
   FileWrite(OutputFile, `UserID   Status   Full Name   Description   Last logon time   Last change time   ID Creation time   Account Options   Manager   Office   Member of...`)
FileClose(OutputFile)
ErrorMode(@OFF)
For i = 1 To ItemCount(sUsers,@TAB)
   AcctOptn = ``
   Status = `Enabled`
   User = ItemExtract(i,sUsers,@TAB)
   AccountOptions = dsGetProperty(User, `userAccountControl`)
   Orig_AccountOptions = AccountOptions
   If AccountOptions > 16777216 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`TRUSTED_TO_AUTH_FOR_DELEGATION`)
         Else
            AcctOptn = StrCat(AcctOptn,`;TRUSTED_TO_AUTH_FOR_DELEGATION`)
      EndIf
      AccountOptions = AccountOptions - 16777216
   EndIf
   If AccountOptions >= 8388608 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`PASSWORD_EXPIRED`)
         Else
            AcctOptn = StrCat(AcctOptn,`;PASSWORD_EXPIRED`)
      EndIf
      AccountOptions = AccountOptions - 8388608
   EndIf
   If AccountOptions >= 4194304 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`DONT_REQ_PREAUTH`)
         Else
            AcctOptn = StrCat(AcctOptn,`;DONT_REQ_PREAUTH`)
      EndIf
      AcctOptn = StrCat(AcctOptn,`DONT_REQ_PREAUTH`)
      AccountOptions = AccountOptions - 4194304
   EndIf
   If AccountOptions >= 2097152 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`USE_DES_KEY_ONLY`)
         Else
            AcctOptn = StrCat(AcctOptn,`;USE_DES_KEY_ONLY`)
      EndIf
      AccountOptions = AccountOptions - 2097152
   EndIf
   If AccountOptions >= 1048576 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`NOT_DELEGATED`)
         Else
            AcctOptn = StrCat(AcctOptn,`;NOT_DELEGATED`)
      EndIf
      AccountOptions = AccountOptions - 1048576
   EndIf
   If AccountOptions >= 524288 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`TRUSTED_FOR_DELEGATION`)
         Else
            AcctOptn = StrCat(AcctOptn,`;TRUSTED_FOR_DELEGATION`)
      EndIf
      AccountOptions = AccountOptions - 524288
   EndIf
   If AccountOptions >= 262144 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`SMARTCARD_REQUIRED`)
         Else
            AcctOptn = StrCat(AcctOptn,`;SMARTCARD_REQUIRED`)
      EndIf
      AccountOptions = AccountOptions - 262144
   EndIf
   If AccountOptions >= 131072 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`MNS_LOGON_ACCOUNT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;MNS_LOGON_ACCOUNT`)
      EndIf
      AccountOptions = AccountOptions - 131072
   EndIf
   If AccountOptions >= 65536 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`DONT_EXPIRE_PASSWORD`)
         Else
            AcctOptn = StrCat(AcctOptn,`;DONT_EXPIRE_PASSWORD`)
      EndIf
      AccountOptions = AccountOptions - 65536
   EndIf
   If AccountOptions >= 8192 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`SERVER_TRUST_ACCOUNT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;SERVER_TRUST_ACCOUNT`)
      EndIf
      AccountOptions = AccountOptions - 8192
   EndIf
   If AccountOptions >= 4096 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`WORKSTATION_TRUST_ACCOUNT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;WORKSTATION_TRUST_ACCOUNT`)
      EndIf
      AccountOptions = AccountOptions - 4096
   EndIf
   If AccountOptions >= 2048 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`INTERDOMAIN_TRUST_ACCOUNT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;INTERDOMAIN_TRUST_ACCOUNT`)
      EndIf
      AccountOptions = AccountOptions - 2048
   EndIf
   If AccountOptions >= 512 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`NORMAL_ACCOUNT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;NORMAL_ACCOUNT`)
      EndIf
      AccountOptions = AccountOptions - 512
   EndIf
   If AccountOptions >= 256 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`TEMP_DUPLICATE_ACCOUNT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;TEMP_DUPLICATE_ACCOUNT`)
      EndIf
      AccountOptions = AccountOptions - 256
   EndIf
   If AccountOptions >= 128 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`ENCRYPTED_TEXT_PWD_ALLOWED`)
         Else
            AcctOptn = StrCat(AcctOptn,`;ENCRYPTED_TEXT_PWD_ALLOWED`)
      EndIf
      AccountOptions = AccountOptions - 128
   EndIf
   If AccountOptions >= 64 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`PASSWD_CANT_CHANGE`)
         Else
            AcctOptn = StrCat(AcctOptn,`;PASSWD_CANT_CHANGE`)
      EndIf
      AccountOptions = AccountOptions - 64
   EndIf
   If AccountOptions >= 32 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`PASSWD_NOTREQD`)
         Else
            AcctOptn = StrCat(AcctOptn,`;PASSWD_NOTREQD`)
      EndIf
      AccountOptions = AccountOptions - 32
   EndIf
   If AccountOptions >= 16 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`LOCKOUT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;LOCKOUT`)
      EndIf
      AccountOptions = AccountOptions - 16
   EndIf
   If AccountOptions >= 8 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`HOMEDIR_REQUIRED`)
         Else
            AcctOptn = StrCat(AcctOptn,`;HOMEDIR_REQUIRED`)
      EndIf
      AccountOptions = AccountOptions - 8
   EndIf
   If AccountOptions >= 2 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`ACCOUNTDISABLE`)
         Else
            AcctOptn = StrCat(AcctOptn,`;ACCOUNTDISABLE`)
      EndIf
      AccountOptions = AccountOptions - 2
      Status = `Disabled`
   EndIf
   If AccountOptions >= 1 Then
      If AcctOptn == `` Then
            AcctOptn = StrCat(AcctOptn,`SCRIPT`)
         Else
            AcctOptn = StrCat(AcctOptn,`;SCRIPT`)
      EndIf
      AccountOptions = AccountOptions - 1
   EndIf
   If AccountOptions <> 0 Then
      Message(`Something doesn't add up`,StrCat(`User = `,User,@CRLF,`AcctOptn = `,AcctOptn,@CRLF,`AccountOptions = `,AccountOptions,@CRLF,`Orig_AccountOptions = `,Orig_AccountOptions))
      Exit
   EndIf
   ManagerName = ``
   ManagerName = dsGetProperty(StrCat(StrCat(`LDAP://`,Company),`/`,dsGetProperty(User, `manager`)), `displayName`)
   If ManagerName == 0 Then ManagerName = ``
   Result = StrCat(dsGetProperty(User, `sAMAccountName`),@TAB,Status,@TAB,dsGetProperty(User, `cn`),@TAB,dsGetProperty(User, `description`),@TAB,dsGetProperty(User, `lastLogonTimestamp`),@TAB,dsGetProperty(User, `WhenChanged`),@TAB,dsGetProperty(User, `createTimeStamp`),@TAB,AcctOptn,@TAB,ManagerName,@TAB,dsGetProperty(User, `physicalDeliveryOfficeName`),@TAB,dsGetProperty(User, `memberOf`))
   OutputFile = FileOpen(OutputFileName, "Append")
      FileWrite(OutputFile, Result)
   FileClose(OutputFile)
Next
ErrorMode(@CANCEL)
Run("Notepad",OutputFileName)

Article ID:   W17555
Filename:   Traverse OU and Generate a Report of Objects.txt
File Created: 2010:09:10:12:38:36
Last Updated: 2010:09:10:12:38:36