[英]How I can update the extensionAttribute3 Attribute in Active Directory with Powershell class in c#?
hi i want update the active directory with powershell and asp.net for this i use this code: 嗨,我想用powershell和asp.net更新活动目录,我使用此代码:
costCenter = "777"; costCenter =“777”;
public string SetADUser(string Auth_Password,string sAMAccountName, string CostCenter)
{
Security key = new Security();
try
{
SecureString pw = new SecureString();
foreach (char x in Auth_Password)
{
pw.AppendChar(x);
}
InitialSessionState initial = InitialSessionState.CreateDefault();
Environment.SetEnvironmentVariable("ADPS_LoadDefaultDrive", "0");
initial.ImportPSModule(new string[] { "ActiveDirectory" });
PSCredential crend = new PSCredential(ADNAME, pw);
using (Runspace runspace = RunspaceFactory.CreateRunspace(initial))
{
runspace.Open();
using (Pipeline p = runspace.CreatePipeline())
{
Command command = new Command("Set-ADUser");
command.Parameters.Add("Identity", sAMAccountName);
command.Parameters.Add("Replace","@{extensionAttribute3=" + "'" + CostCenter + "'" + "}"); ?? what is here wrong
//command.Parameters.Add("extensionAttribute3", CostCenter);
command.Parameters.Add("Credential", crend);
p.Commands.Add(command);
p.Invoke();
return string.Empty;
}
}
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
}
}
this is the error: 这是错误:
The Parameter "Replace" can not bind. The Value "@{extensionAttribute3='777'}" from typ "System.String" can not convert to "System.Collections.Hashtable".
How I can do this? 我怎么能这样做?
@{}
is a powershell-shortcut to create a hashtable. @{}
是创建哈希表的PowerShell快捷方式。 In C# you should create a hashtable in the normal C#-way. 在C#中,您应该在正常的C#-way中创建一个哈希表。
var ht = new Hashtable { { "extensionAttribute3", CostCenter } };
command.Parameters.Add("Replace", ht);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.