2014-09-02 78 views
1

我有這段代碼,我遠程發送PowerShell命令「日期」到我的交換服務器(server01),它的工作原理,我收到一個消息框的結果。C#連接到Powershell,但我需要連接到Exchange管理外殼

但是,如果我發送命令「Get-Mailbox」,則debbuger將停止並顯示以下錯誤:術語'Get-Mailbox'不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。

如果我去server01並運行powershell並執行「Get-Mailbox danielr」與我得到的錯誤相同。但是,Exchange命令行管理程序可以執行Get-Mailbox命令。

所以,我想我已經連接到Window Powershell cmd ..但是,要執行「Get-Mailbox danielr」和其他Exchange管理命令,我必須連接到Exchange命令行管理程序。

我需要改變什麼才能使它工作? (連接到Exchange命令行管理,而不是PowerShell的。 非常感謝!

public void CreateMailBoxExchange() 
    { 
     string loginName = "administrator"; 
     string loginPassword = "123456"; 
     SecureString ssLoginPassword = new SecureString(); 
     foreach (char x in loginPassword) 
      ssLoginPassword.AppendChar(x); 


     PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword); 

     // Set the connection Info 
     WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", remoteMachineCredentials); 

     connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate; 
     //connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; 

     Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo); 

     PowerShell powershell = PowerShell.Create(); 
     PSCommand command = new PSCommand(); 
     command.AddCommand("date"); 
     //command.AddCommand("Get-Mailbox"); 
     //command.AddParameter("Identity", "danielr"); 

     powershell.Commands = command; 
     try 
     { 
      // open the remote runspace 
      runspace.Open(); 
      // associate the runspace with powershell 
      powershell.Runspace = runspace; 
      // invoke the powershell to obtain the results 
      powershell.Invoke(); 
      var results = powershell.Invoke(); 
      runspace.Close(); 
      foreach (PSObject obj in results) 
      { 
       StringBuilder stringBuilder = new StringBuilder(); 
       //stringBuilder.AppendLine(obj.ToString()); 
       MessageBox.Show(obj.ToString()); 
      } 

     } 
     finally 
     { 
      // dispose the runspace and enable garbage collection 
      runspace.Dispose(); 
      runspace = null; 
      // Finally dispose the powershell and set all variables to null to free 
      // up any resources. 
      powershell.Dispose(); 
      powershell = null; 
     } 

回答

0

你應該能夠只包括Exchange管理管理單元之前嘗試獲取郵箱命令。這裏是我該怎麼辦在PowerShell中:

Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin" 
1

根據您正在運行的Exchange版本,您可能需要執行以下代碼:

對於2007年可置換

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin 

對於可置換2010

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010 

對於EXCH 2013(試試這個)

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn 
0

如果你從你的代碼提示,則Exchange服務器是遠程的,你不得不使用像:

string loginName = "administrator"; 
string loginPassword = "123456"; 
SecureString ssLoginPassword = new SecureString(); 
foreach (char x in loginPassword) 
    ssLoginPassword.AppendChar(x); 

PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword); 
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", remoteMachineCredentials); 
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate; 
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
+0

嗨。非常感謝你的幫助。 – 2014-09-02 16:57:33

+0

你能幫我告訴我,我應該把代碼放在哪裏(在我之間)請! – 2014-09-02 17:15:32

+0

好的,讓我試試。感謝邁克爾 – 2014-09-02 17:48:31