ADSI和C# [英] ADSI and C#

查看:69
本文介绍了ADSI和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试重命名管理员登录,我想知道我的

方法是否正确:

void RenameUsr(string hostname,string admusr)

{


DirectoryEntry entry = new DirectoryEntry(" WinNT:/ /" + hostname +" /" +

" Administrator" +",user");


entry.Properties [" UserPrincipalName"]。值= admusr;


entry.CommitChanges();


}


谢谢,


Victor

Hi,

I''m trying to rename the Administrator Login, and i want to know if my
approach is right:
void RenameUsr(string hostname, string admusr)

{

DirectoryEntry entry = new DirectoryEntry("WinNT://" + hostname + "/" +
"Administrator" + ",user");

entry.Properties["UserPrincipalName"].Value = admusr;

entry.CommitChanges();

}

Thanks,

Victor

推荐答案

>我正在尝试重命名管理员登录,我想知道我的
>I''m trying to rename the Administrator Login, and i want to know if my
方法是否正确:




你是什么意思重命名?什么名字(有很多!)你是否想要改变?b $ b?实际的登录名? LDAP对象名称?

显示名称?


另外,你是在本地机器上运行还是在网络上运行?如果你是网络上的b $ b,我强烈建议使用LDAP代替

弃用的WinNT提供商。


Marc



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


嗨Marc,

我想重命名登录名。现在我改变了我的方法,现在我正在使用

WMI。但我的问题是:这个代码在W2k中不起作用,只是在XP和2003中。

你知道我怎么能在W2k环境中做到这一点?

看我的代码:


void RenameUsr(字符串主机名,字符串admusr)


{

尝试


{


ConnectionOptions oConn = new ConnectionOptions();


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


string myQuery =" Select * from Win32_UserAccount WHERE Domain =" +"''" +

主机名+"''" + 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(在queryCollection1中管理对象)


{

尝试


{


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"]。值;


if(ret!= 0)


{


MessageBox.Show(" Error" + ret.ToString()+"试图重命名用户);


}


}


}


catch(例外情况)


{


MessageBox.Show(主机名+"" + admusr +"" + ez.Message);


}


}


}


catch(例外zz)


{


MessageBox.Show(主机名+" " + admusr +"" + zz.Message);


}


}


}


在此先感谢,


Victor


" Marc Scheuner [ MVP ADSI] <米******** @ inova.SPAMBEGONE.ch>。在留言中写道

news:s4 ******************************** @ 4ax.com ...
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.********@inova.SPAMBEGONE.ch> wrote in message
news:s4********************************@4ax.com...
我正在尝试重命名管理员登录,我想知道我的
方法是否正确:
I''m trying to rename the Administrator Login, and i want to know if my
approach is right:



你是什么意思重命名?什么名字(有很多!)你想改变吗?实际的登录名? LDAP对象名称?
显示名称?

此外,您是在本地计算机上运行还是在网络中运行?如果您在网络上,我强烈建议您使用LDAP而不是弃用的WinNT提供商。

Marc



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



>我想重命名登录名。


再次:你是在处理本地机器和本地帐户,还是

这是一个网络/网络域中的帐户?


AFAIK,WMI是设计的只读 - 我认为你不能更新

任何通过它,真的。


如果您正在处理域中的域和用户帐户,那么

最好的选择是使用ADSI和LDAP提供程序 - 在

问题中找到用户(通过了解他的LDAP路径,或者搜索

),以及然后更新相应的名称(在您的情况下:

" sAMAccountName"),并将更改保存回商店。


类似于:


DirectoryEntry deUser = new

Dir ectoryEntry(" LDAP:// cn = JohnDoe,cn = Users,dc = fabrikam,dc = com");

deUser.Properties [" sAMAccountName"]。Value = " new_logon_name";

deUser.CommitChanges();


Marc
>I want to rename the logon name.

Again: are you dealing with a local machine and a local account, or is
this an account on a network / in a network domain?

AFAIK, WMI is read-only by design - I don''t think you can update
anything through it, really.

If you''re dealing with a domain and a user account in a domain, your
best choice is to use ADSI and the LDAP provider - find the user in
question (either by just knowing his LDAP path, or by searching for
it), and then update the appropriate name (in your case: the
"sAMAccountName"), and save the changes back to the store.

Something like:

DirectoryEntry deUser = new
DirectoryEntry("LDAP://cn=JohnDoe,cn=Users,dc=fabrikam,dc=com");

deUser.Properties["sAMAccountName"].Value = "new_logon_name";
deUser.CommitChanges();

Marc


这篇关于ADSI和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆