如何使用C#中的System.Management.Automation删除ADUser?

如何使用C#中的System.Management.Automation删除ADUser?,c#,asp.net,powershell,active-directory,automation,C#,Asp.net,Powershell,Active Directory,Automation,我使用System.Management.Automation与powershell和c#一起工作,我想使用Remove ADUser命令,但我得到一个错误:这里是我的代码 public bool RemoveADUser(string sAMAccountName) { Security key = new Security(); try { ADPASSWORD = key.Pass

我使用System.Management.Automation与powershell和c#一起工作,我想使用Remove ADUser命令,但我得到一个错误:这里是我的代码

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();但我得到的信息是:

德语:

费勒在一个月前就到了。在开始与keine Benutzer的互动之前,请先看主机程序。弗尔登·维苏克,最好的一个节目,主持人福尔根德·梅尔登·维文德:你是主持人吗

英文:


带有命令提示符的命令出错。不支持用户交互主机程序或命令类型。对于实验,为了请求确认,使用的主机发出的以下消息是:是否要执行此操作?

调用此命令的方式导致命令提示提供更多信息,但您的C#引擎“主机”未实现允许PowerShell引擎提示用户获取更多信息的所需界面。我看到您输入了密码,但此命令上的文档确实显示:

如果为此参数指定用户名,cmdlet将提示输入 密码

我想知道你的证件是否有问题