Re: ADSI and C#



Hi Marc,
I want to rename the logon name. Now i changed my approach and now i'm using
WMI. But my problem is: This code does not work in W2k, just in XP and 2003.
Do you know how can i do it in a W2k Enviroment ?
Look my code:

void RenameUsr(string hostname, string admusr)

{


try

{

ConnectionOptions oConn = new ConnectionOptions();

string myMachine = @"\\" + hostname + @"\root\cimv2" ;

string myQuery = "Select * from Win32_UserAccount WHERE Domain = " + "'" +
hostname + "'" + " AND SIDType = 1 ";

ManagementScope oMs = new ManagementScope(myMachine, oConn);

ObjectQuery oQuery = new ObjectQuery(myQuery);

ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMs,oQuery);

ManagementObjectCollection queryCollection1 = oSearcher.Get();

foreach( ManagementObject mo in queryCollection1 )

{


try

{

string sLogin = mo["Name"].ToString();

if(sLogin.StartsWith("Admin"))

{

ManagementBaseObject iPar = mo.GetMethodParameters("Rename");

iPar["Name"] = admusr;

ManagementBaseObject outPar= mo.InvokeMethod("Rename", iPar, null);

uint ret = (uint)outPar.Properties["ReturnValue"].Value;

if(ret != 0)

{

MessageBox.Show("Error " + ret.ToString() + " trying to rename user");

}

}

}

catch(Exception ez)

{

MessageBox.Show(hostname + " " + admusr + " " + ez.Message);

}

}

}

catch(Exception zz)

{

MessageBox.Show(hostname + " " + admusr + " " + zz.Message);

}

}

}

Thanks in advance,

Victor

"Marc Scheuner [MVP ADSI]" <m.scheuner@xxxxxxxxxxxxxxxxxxx> wrote in message
news:s4v171l7lvk2hssn6i6eiros5d4djqkpfa@xxxxxxxxxx
> >I'm trying to rename the Administrator Login, and i want to know if my
> >approach is right:
>
> What do you mean by "rename"? What name (there are plenty!) do you
> want to change? The actual logon name? The LDAP object name? The
> display name ?
>
> Also, are you running on a local machine, or in a network?? If you're
> on a network, I'd STRONGLY recommend using LDAP instead of the
> deprecated WinNT provider.
>
> Marc


.



Relevant Pages

  • Re: New employee, same computer -- what to do?
    ... By "local account" I mean there is a local username that is created when ... It is not considered good practice to rename user accounts to deal ... good domain logon password, but everyone has access to all the shared ... course Jane will use Susie's old computer and needs access to the ...
    (microsoft.public.windows.server.active_directory)
  • Re: Corrupt User Profile
    ... Logon as local administrator, open Windows Explorer and navigate to the; ... \Documents and Settings ... look for the folder named for your logon and rename it to something ... you need from your old profile to the new one. ...
    (microsoft.public.win2000.registry)
  • Re: Would like to rename folders in Documents and Settings
    ... | to change to match my logon. ... Can I rename that cryptic directory somehow to match my name? ... The name is store in the User Registry. ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Quick Launch gone
    ... If you can't get it then either create a new user and logon with that or rename your profile Then when you next logon a new profile will be created from an image in \default user. ... Copy the folder contents not the folders themselves. ...
    (microsoft.public.win2000.general)
  • Re: XNews Question
    ... > Marc L. wrote: ... > If you are worried, rename the old exe, you can always switch back. ...
    (news.software.readers)