2016-04-06 74 views
1

我想通過C#項目與我的機器上安裝的Microsoft.Exchange.Management.PowerShell.E2010進行交互。C#Powershell - Exchange管理{「值不能爲空。 r nParameter name:serverSettings」}

我的本地計算機是Windows Server 2012 R2標準版,安裝了彙總更新14的Exchange Server 2010 SP3。

我使用的是4.5的.NET Framework(降級到舊版本是不可能的)

 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(); 

     connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes. 
     connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute. 

     Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
     runspace.Open(); 
     using (PowerShell ps = PowerShell.Create()) 
     { 
      ps.Runspace = runspace; 

      ps.AddCommand("Add-PsSnapIn"); 
      ps.AddArgument("Microsoft.Exchange.Management.PowerShell.E2010"); 

      var results = ps.Invoke(); 

      try 
      { 
       ps.AddCommand("Get-MailBox"); 

       results = ps.Invoke(); 
      } 
      catch (Exception e) 
      { 

      } 
     } 
     runspace.Close(); 
  • 我打開遠程shell會話(針對性我的本地機器上)。
  • 添加Exchange管理PsSnapIn,以便訪問交換命令。
  • 最後我執行我的Exchange管理命令。

\!/問題是在最後一步,results = ps.Invoke();拋出與消息"Value cannot be null.\r\nParameter name: serverSettings"一個System.Management.Automation.RemoteException

你們有什麼想法嗎?

謝謝你的時間。

回答

1

最近幾天我一直在爲此而戰。我知道這個問題已經過去了幾個月,但我想我會分享我終於找到的解決方案。在您的.config中,您需要在startup標記上將屬性設置爲true。像這樣:

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/> 
</startup> 

使用這個,我成功地運行了一個Get-Mailbox命令,同時面向.NET 4.6.2。

+0

謝謝,我可以試試! –

相關問題