ActiveDirectoryMail

Ukázka práce s Active Directory

Popis

Skript načte (a zobrazí) veškeré emailové adresy (obsahující "zavináč") vč. (zobrazovaných) jmen uživatelů.
declare function ActiveDirectoryObject_Create dll "ActualDocument" (string, string, string, string): Integer
declare function ActiveDirectoryObject_Free dll "ActualDocument" (Integer): Integer

declare function ADs_GetAsString dll "ActualDocument" (Integer, string, var Integer): string

declare function DirectorySearch_ExecuteSearch dll "ActualDocument" (Integer, string, string): Integer
declare function DirectorySearch_GetNextRow dll "ActualDocument" (Integer, var Integer): Boolean
declare function DirectorySearch_GetColumnAsString dll "ActualDocument" (Integer, string, var Integer): string

declare function GetLastErrorMessage dll "ActualDocument" (): string

script ActiveDirectoryMail (): Boolean
var
  Server: string
  UserName: string
  Password: string

  ADs: Integer
  ReturnCode: Integer
  DefaultNamingContext: string
  DirectorySearch: Integer
  Mail: string
  DisplayName: string
  ErrorMessage: string
begin
  Server := "server"
  UserName := "user"
  Password := "pass"

  // --- Get default naming context ---
  ADs := ActiveDirectoryObject_Create("TADs", "LDAP://" + Server + "/RootDSE", UserName, Password)
  DefaultNamingContext := ADs_GetAsString(ADs, "defaultNamingContext", ReturnCode)
  if ReturnCode = 0 then
    ErrorMessage := GetLastErrorMessage()
    write(ErrorMessage)
  end
  ActiveDirectoryObject_Free(ADs)

  // --- Get users with email address ---
  DirectorySearch := ActiveDirectoryObject_Create("TDirectorySearch", "LDAP://" + Server + "/" + DefaultNamingContext, UserName, Password)

  ReturnCode := DirectorySearch_ExecuteSearch(DirectorySearch, "mail=*@*", "displayName, mail")
  if ReturnCode = 0 then
    ErrorMessage := GetLastErrorMessage()
    write(ErrorMessage)
  end

  while DirectorySearch_GetNextRow(DirectorySearch, ReturnCode) do

    DisplayName := DirectorySearch_GetColumnAsString(DirectorySearch, "displayName", ReturnCode)
    Mail := DirectorySearch_GetColumnAsString(DirectorySearch, "mail", ReturnCode)

    write(DisplayName + ": " + Mail)
  end

  ActiveDirectoryObject_Free(DirectorySearch)

  Result := True
end