2013-10-10 27 views
0

我用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程序或命令類型的命令

誤差不被支持的用戶交互。在實驗中,請求確認,從主機使用下面的消息是:你要執行此操作?

回答

1

一些與你是如何調用這個命令是導致命令提示輸入更多的信息,但你的C#引擎「主機」未實現所需的接口確實允許PowerShell引擎提示用戶獲得更多信息。我看到你在一個密碼通過,但這一命令的文檔不這樣說:

If you specify a user name for this parameter, the cmdlet prompts for a password.

我不知道是否有人與你PSCredential的borked?