我用System.Management.Automation用於與PowerShell和C#的工作,我想使用Remove-ADUser便有命令,但我得到一個錯誤:在這裏,我的代碼。我怎麼能在C#中刪除ADUser便有與System.Management.Automation?
public bool RemoveADUser(string sAMAccountName) {
Security key = new Security();
try
{
ADPASSWORD = key.PasswordDecrypt("vnPYuGoPSvrSjDC+/5lUTQ==");
//ADPASSWORD = "vnPYuGoPSvrSjDC+/5lUTQ==";
SecureString SECUREADADMINPASSWORD = new SecureString();
foreach (char x in sAMAccountName)
{
SECUREADADMINPASSWORD.AppendChar(x);
}
SecureString pw = new SecureString();
foreach (char x in ADPASSWORD)
{
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("Remove-ADUser");
command.Parameters.Add("Identity", sAMAccountName);
command.Parameters.Add("Credential", crend);
p.Commands.Add(command);
p.Invoke();
return true;
}
}
}
catch (Exception)
{
return false;
}
finally
{
}
}
代碼去p.Invoke();在德國
:在命令提示符下的命令
錯誤,但後來我得到這個消息。從主機程序,或指令類型被支持沒有用戶交互。在實驗中,請求確認,使用主機以下消息:是否要執行此操作?
在英文:在與命令主機prompt.The程序或命令類型的命令
誤差不被支持的用戶交互。在實驗中,請求確認,從主機使用下面的消息是:你要執行此操作?