2016-11-22 43 views
1

林使用PowerShell試圖訪問在C# 這裏使用PowerShell中的Exchange服務器的代碼我試圖使用訪問Exchange在C#

  WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null); 
      ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
      using (Runspace runspace = RunspaceFactory.CreateRunspace(ConnectionInfo)) 
      { 
       using (PowerShell PS_Instance = PowerShell.Create()) 
       { 
        PS_Instance.AddCommand("Get-MailboxPermission -Identity UAT_TestGeneric"); 

        runspace.Open(); 
        PS_Instance.Runspace = runspace; 
        Collection<PSObject> results = PS_Instance.Invoke(); 

        if (PS_Instance.Streams.Error.Count > 0) 
        { 
         //Couldnt Connect To Server 
        } 
        else 
        { 
         //Do Stuff 
        } 
       } 

我不斷收到此錯誤信息

的WinRM客戶端從遠程的 WS管理服務收到HTTP狀態碼403。有關更多信息,請參閱 about_Remote_Troubleshooting幫助主題。

我已經檢查了exchange服務器的powershell IIS文件夾,它不需要SSL,如https://support.microsoft.com/en-au/kb/2276957所示。

有沒有可以提供幫助的人? 謝謝。

+0

您可能想嘗試使用Powershell首先進行連接,以測試您是否具有正確的先決條件並檢查Auth是否正確等https: //technet.microsoft.com/en-us/library/dd297932(v=exchg.141).aspx –

回答

1

由於錯誤403意味着沒有別的「訪問被拒絕」我認爲遠程PowerShell沒有啓用您正在使用的任務用戶。嘗試啓用通過(如寫入MS文檔here中):

set-user <your user account> -RemotePowerShellEnabled $True 

另一個有用的源來解決你的問題是MS文檔here

0

Incase其他人看到這個。 我解決了這個問題,因爲URI是錯誤的

WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null); 

的URI必須是@ 「http://myexchangeserver.com/Powershell」。 因此,儘管PowerShell在服務器上正確設置,您需要連接到正確的IIS文件夾路徑