Link to home
Start Free TrialLog in
Avatar of QC20N
QC20NFlag for Denmark

asked on

Need info to start code adsi in delphi.

Hi,

I would like to learn how to code ADSI i delphi.NET. So I need to know where I can find information about this. My knowledge on ADSI is zero. So I need the info to be novice info and if you could provide some code, I could "play" with, it will be cool. My version of Delphi is Delphi 2007.

Hope you can help me.
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

this is pretty good starter:

http://www.agnisoft.com/white_papers/active_directory.asp

ziolko.
Avatar of QC20N

ASKER

I have looked at this, but I can't figure it out the different commands to get infomation from a user object. How do I do that?
here are some snippets from my code

SRVADSERVER is the domain server running Active Directory

you need to import the Active Directory Type library (ActiveDs_TLB)

and the adshlp library


procedure TImportUsers.CallbackDomain(Obj: IAds);
begin
  if (Obj.Class_ = 'organizationalUnit') and not Terminated then
    ADsEnumerateObjects(Obj.ADsPath, CallbackOrganizationalUnit, 'organizationalUnit,user');
end;
 
procedure TImportUsers.CallbackOrganizationalUnit(Obj: IAds);
  function GetProp(UserObj: IAdsUser; PropName: string): string;
  begin
    Result := '';
    try
      Result := UserObj.Get(propName);
    except
    end;
  end;
var
  SubObj: IAds;
  aUser: IADsUser;
  aName, aFirstName, aLastName, aFullname, aDescr, aEmail, aDomain, aPath: string;
begin
  if not Terminated then
  begin
    if Obj.QueryInterface(IID_IADsContainer, SubObj) = S_OK then
      if not Terminated then
        ADsEnumerateObjects(SubObj as IAdsContainer, CallbackOrganizationalUnit, 'organizationalUnit,user');
    if SameText(Obj.Class_, 'User') then
      if not Terminated then
      begin
        aUser := Obj as IADsUser;
        aName := GetProp(aUser, 'samAccountName');
        aFirstName := GetProp(aUser, 'FirstName');
        aLastName := GetProp(aUser, 'LastName');
        aFullName := GetProp(aUser, 'FullName');
        aDescr := GetProp(aUser, 'Description');
        aEmail := GetProp(aUser, 'mail');
        aDomain := ParseDomain(Obj.ADsPath);
        aPath := Obj.ADsPath;
        ImportUser(1, aName, aFirstName, aLastName, aFullName, aDescr, aEmail, aDomain, aPath);
      end;
  end;
end;
 
procedure TImportUsersThread.DoImport;
var DomainPath: WideString;
begin
  DomainPath := 'LDAP://SRVADSERVER/';
  if not Terminated then
    ADsEnumerateObjects(DomainPath, CallbackDomain);
end;

Open in new window

Avatar of QC20N

ASKER

Looks great. If I should get properties on a Computerobject, how should I accomplish that?
same way

or use function ReadProperty


procedure TImportComputer.CallbackOrgComputer(Obj: IAds);
begin
  if SameText(Obj.Class_, 'Computer') then 
    //ImportComputer(1, Obj.Name, Obj.ADsPath)
   ;
end;
 
procedure TImportComputer.CallbackOrganizationalUnit(Obj: IAds);
var SubObj: IAds;
  MemObj: OleVariant;
begin
  if not Terminated then
  begin
    if Obj.QueryInterface(IID_IADsContainer, SubObj) = S_OK then
    begin
      if not Terminated then
        ADsEnumerateObjects(SubObj as IAdsContainer, CallbackOrganizationalUnit, '');
    end else if SameText(Obj.Class_, 'Computer') then
    begin
      if not Terminated then
        // ImportComputer(1, Obj.Name, Obj.ADsPath)
        ;
    end;
  end;
end;
 
procedure TImportComputer.CallbackDomain(Obj: IAds);
begin
  if not Terminated then
    if Obj.Class_ = 'organizationalUnit' then
      ADsEnumerateObjects(Obj.ADsPath, CallbackOrganizationalUnit, 'organizationalUnit,computer');
end;
 
function TImportComputer.ReadProperty(Obj: IAds; propName: string = 'businessRoles'): string;
var
  prop, subprop: OleVariant;
  Temp: string;
  I, J, m, n: integer;
begin
  Temp := '';
  prop := VarArrayOf([propname]);
  Obj.GetInfoEx(prop, 0);
  try
    prop := Obj.GetEx(propname);
    n := VarArrayDimCount(prop);
    if n = 0 then
      Temp := Temp + VarToStr(prop)
    else if n = 1 then
      for I := VarArrayLowBound(prop, 1) to VarArrayHighBound(prop, 1) do
      begin
        subprop := prop[I];
        m := VarArrayDimCount(subprop);
        if m = 0 then
          Temp := Temp + VarToStr(subprop)
        else if m = 1 then
        begin
          for J := VarArrayLowBound(subprop, 1) to VarArrayHighBound(subprop, 1) do
            Temp := Temp + Chr(StrToInt(VarToStr(subprop[J])));
        end;
      end;
  except
    //on E: Exception do
    //  ShowMessage('error reading property ' + propname + ' : ' + E.Message);
  end;
  if (Temp <> '') and (Temp[1] <> '"') and (Temp[length(Temp)] <> '"') then
    Temp := AnsiQuotedStr(Temp, '"');
  Result := Temp;
end;

Open in new window

Avatar of QC20N

ASKER

I'm sure your code is usefull. But I can't see how I can for an exampel get the "Managed By" prop on the computerobject.

Where can I see all the different properties on a use and on a computerobject?
Avatar of QC20N

ASKER

Well, I found all the properties on a object.

But I can't figure it out how to find the properties on a specific computerobject with your code.

For me it seems that you run throug all user objects.

Is that true?
Avatar of QC20N

ASKER

Could you tell me what to do?
can you open the AD using ad.msc ?
Avatar of QC20N

ASKER

What is "ad.msc"?

:)
ad.msc

is the mcc snapin for reading info from Active Directory
It comes with a Windows Adminstration Tools Pack

or use
http://www.ldapadministrator.com/
Avatar of QC20N

ASKER

Doh, yes, of course. Yes, that part do I have installed. And can see our AD domain.
Avatar of QC20N

ASKER

Geert Gruwez:
You are there?
um now and then, a little busy lately with work ...
For me it seems that you run throug all user objects.

Is that true?

Yes that's true
I copy them to a database and then use the database for reading ...
a lot faster, and makes all other applications independent of AD
Avatar of QC20N

ASKER

But you know how to get all info from the computerobjects?
you mean the below code ?

read the first entry by ziolko ...
var obj : IAds;
 s : WideString;
 cls : IADsClass;
 cont : IADsContainer;
 i : integer;
begin
 AdsGetObject('LDAP://CN=Users,DC=AGNISOFT,DC=COM', IADs,  obj );
 s := obj.Get_Schema;
 AdsGetObject(s, IADsClass, cls );
 if VarIsArray(cls.MandatoryProperties) then
 begin
  for i := VarArrayLowBound(cls.MandatoryProperties,1) to
   VarArrayHighBound(cls.MandatoryProperties,1) do
  begin
   s := cls.MandatoryProperties[i];
   ShowMessage('MANDATORY:' + s);
  end;
 end;
if VarIsArray(cls.OptionalProperties) then
begin
  for i := VarArrayLowBound(cls.OptionalProperties,1) to
    VarArrayHighBound(cls.OptionalProperties,1) do
begin
  s := cls.OptionalProperties[i];
  ShowMessage('Optional:' + s);
 end;
end;

Open in new window

you would off course change your 7th line:


 AdsGetObject('LDAP://CN=ComputerName,OU=Computers,DC=AGNISOFT,DC=COM', IADs,  obj );

via the schema you can find what attributes (or "properties" are bound to the object
Avatar of QC20N

ASKER

Yes, I have look at it.

What I'm trying to accomplish is to find computers that a user is "ManagedBy".

You know, there is a tab called "managedby". :)

Maybe I should use a ADOQuery instead? But it could be nice to find the solution by using ADSI.
to find computers that a user ???
to find a computer that is managedBy should work


use
function ReadProperty(Obj: IAds; propName: string = 'businessRoles'): string;

var aManagedBy: string;
begin
  aManagedBy := ReadProperty(Obj, 'managedBy');

be aware !
managedBy is case sensitive
no capital M !
Avatar of QC20N

ASKER

Need to find who is the manager of the computer by using the tab on the computerobject called "managedby"
Avatar of QC20N

ASKER

How should I bind to the object?
you can't "the tab" when you're looking in ADSI
this is just on the GUI.

look for the properties ...

there is also "manager"
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QC20N

ASKER

Yes, it works on the user objects. I can get the properties.

But now I need a solution to loop throug all our computersobject and find wich computer the user is managed by.
Avatar of QC20N

ASKER

Well, I found a solution. I use ADOQuery to find who is manager og a computer.

As you say your code run throug all users in the AD. If I just want to find properties on 1 user. How should I do that?
Avatar of QC20N

ASKER

Geert Gruwez is very nive person. Took the time to explain what he mean.
Thx.