Einzelnen Beitrag anzeigen

Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: Unterhaching
11.412 Beiträge
 
Delphi 12 Athens
 
#2

Re: Active Directory: Benutzer/Computer mit Anlagedatum?

  Alt 15. Okt 2007, 14:20
Hi Sharky,

fast einfach. Ein bissle ADO und eine spezielle Funktion (VBGetObject) erledigen die Aufgabe:
Delphi-Quellcode:
procedure TYourForm.btnSearchClick(Sender: TObject);
var
  Conn: _Connection;
  Cmd: _Command;
  RS: _Recordset;
  Affected: OleVariant;
  Line: string;
  Obj: IADsUser;
  procedure RunQuery(Query: string);
  begin
    Cmd.CommandText := Query;
    // run query and return domain list
    RS := Cmd.Execute(Affected, EmptyParam, 0);
    if RS.EOF then
      Exit;
      
    RS.MoveFirst;
    while not RS.EOF do
    begin
      try
        Obj := VBGetObject(RS.Fields.Item[1].Value) as IADsUser;
        Line := Format('%s, %s, %s, %s', [RS.Fields.Item[0].Value, RS.Fields.Item[1].Value, RS.Fields.Item[2].Value, Obj.Parent]);
        mmoReport.Lines.Add(Line);
      except
      end;
      RS.MoveNext;
    end;
  end;
begin
  mmoReport.Clear;

  // create objects
  Conn := CoConnection.Create;
  Cmd := CoCommand.Create;
  // setup objects
  Conn.Provider := 'ADsDSOObject';
  Conn.Open('Active Directory Provider', '', '', 0);
  Cmd.Set_ActiveConnection(Conn);
  Cmd.Properties.Item['Page Size'].Value := 1000;
  Cmd.Properties.Item['Searchscope'].Value := ADS_SCOPE_SUBTREE;

  RunQuery(edtQuery.Text);
Die Funktion VBGetObject:
Delphi-Quellcode:
function VBGetObject(const Name: AnsiString): IDispatch;
var
  BindContext: IBindCtx;
  Moniker: IMoniker;
  Eaten: Integer;
begin
  OleCheck(CreateBindCtx(0, BindContext));
  OleCheck(MkParseDisplayName(BindContext, PWideChar(WideString(Name)), Eaten, Moniker));
  OleCheck(Moniker.BindToObject(BindContext, nil, IDispatch, Result));
end;
Das Format der Query:
Zitat:
SELECT Name, ADsPath, whenCreated FROM 'LDAP://DC=fabrikam,DC=com' WHERE objectCategory='User'
bzw.
SELECT Name, ADsPath, whenCreated FROM 'LDAP://DC=fabrikam,DC=com' WHERE objectCategory='Computer'
Das volle Beispiel findest Du in meinem Blog
Daniel Lizbeth
Ich bin nicht zurück, ich tue nur so
  Mit Zitat antworten Zitat