Comme il n'y a pas beaucoups d'exemple traitant de la modif d'Active Directory via LDAP et ADSI en Delphi sur le net. Je mets à dispo cette petite Unit, afin d'apporter un peu d'aide sur le sujet. :-)
Petite Unit pour :
- Modifier le champ 'description' d'un PC dans Active Directory (Put).
- Deplacement d'un PC d'une OU vers une autre (MoveHere) avec ADSI et LDAP.
Utilise activeds.dll, installé avec Win 2000 et Win XP.
On trouve ActiveDs_TLB.pas sur le Net.
Source / Exemple :
//----------------------------------------------------------------------------------//
// Unite pour gérer AD. Crée le 21/02/05, par stef_cch@yahoo.fr //
// Dernière modif le 21/02/05. //
//----------------------------------------------------------------------------------//
unit LibADSI;
interface
uses
SysUtils,
Variants,
Dialogs,
ComObj, ActiveX,
ActiveDs_TLB; //ADSI
//------------------------------------------------------------------------------
function ADsGetObject(lpszPathName: PWideChar; const riid: TGUID; out obj): HResult; stdcall; external 'activeds.dll';
function ADsOpenObject(lpszPathName: PWideChar; lpszUserName: PWideChar; lpszPassword: PWideChar; dwReserved: LongInt; const riid: TIID; out obj): HResult; stdcall; external 'activeds.dll';
function MOVE_ordi_ds_ActiveDirectory(nom_PC_a_deplacer : string; Type_de_PC : integer): string;
function Change_Description_PC_ADSI ( nom_ordi_a_modif,descript : string) : string; // Modification de la description du PC ds Active Directory
//------------------------------------------------------------------------------
implementation
//------------------------------------------------------------------------------
function MOVE_ordi_ds_ActiveDirectory(nom_PC_a_deplacer : string; Type_de_PC : integer): string;
const MAX_car = 255;
var
UnknownObj: IUnknown;
container : IADsContainer;
nom_pc_loc, ou_dest_loc : string;
myUser, myPassword : string;
SourceName, DestName, NewName : String;
begin
myUser := 'DOMAIN\LOGIN' ;
myPassword := 'PWD' ;
nom_pc_loc := nom_PC_a_deplacer;
DestName := 'LDAP://NomServeur/OU=' + ou_dest_loc + ',OU=...,DC=...,DC=...,DC=...';
SourceName := 'LDAP://NomServeur/CN=' + nom_pc_loc + ',OU=OU_SOURCE,DC=...,DC=...,DC=...';
NewName := ''; //'CN=NOUVEAU_NOM_PC_A_DEPLACER';
try
//OleCheck(ADsGetObject(StringToOleStr(DestName), IID_IADs, UnknownObj)); //connextion sans authentifications
OleCheck( AdsOpenObject( StringToOleStr(DestName),
StringToOleStr(myUser),
StringToOleStr(myPassword), 0, IID_IADsContainer, UnknownObj)); //avec authentif
container := UnknownObj as IADsContainer;
container.MoveHere( StringToOleStr(SourceName), StringToOleStr(NewName) ); //Deplace et renomme
except
on E: EOleException do begin ShowMessage('Source = ' + E.Source + ' Message = ' + E.Message); end;
end;
end;
//------------------------------------------------------------------------------
function Change_Description_PC_ADSI ( nom_ordi_a_modif,descript : string) : string; // Modif la description du PC ds Active Directory
const MAX_car = 255;
var
UnknownObj: IUnknown;
obj : IAds;
myUser, myPassword, nom_pc, DestName : string;
begin
nom_pc := nom_ordi_a_modif;
DestName := 'LDAP://NomServeur/CN=' + nom_pc + ',OU=...,DC=...,DC=...,DC=...';
myUser := 'DOMAIN\LOGIN' ;
myPassword := 'PWD' ;
try
//ADsGetObject(StringToOleStr(DestName), IID_IADs, UnknownObj); //connextion sans authentifications
OleCheck( AdsOpenObject( StringToOleStr(DestName),
StringToOleStr(myUser),
StringToOleStr(myPassword), 0, IID_IADs, UnknownObj)); //avec authentifications
obj := UnknownObj as IADs;
obj.Put('description', descript); //met dans un tampon
obj.SetInfo; //valide la valeur du tampon ds AD. Si Get avant SetInfo Alors cela efface le Put
result := obj.get('description');
except
on E: EOleException do begin ShowMessage('Source = ' + E.Source + ' Message = ' + E.Message); end;
end;
end;
//------------------------------------------------------------------------------
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.