2017-08-20 42 views
0

在Visual Studio中以調試模式運行時,爲本地交換服務器啓用郵箱的代碼工作,但在同一實例上的IIS上部署時失敗。該命令也可從Powershell Console工作 It拋出異常無法連接到服務器訪問被拒絕。從C#powershell啓用郵箱

有人能幫我解決嗎?

PFB從Visual Studio調試模式下運行時工作完全正常,並從在IIS上部署的代碼運行時出現故障段

string connectionUri = strConURI; 
    string loginPassword = Pwd; 
      SecureString secpassword = new SecureString(); 
      foreach (char c in loginPassword) 
      { 
       secpassword.AppendChar(c); 
      } 
      PSCredential credential = new PSCredential(Usercred, secpassword); 

      Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); 
      PowerShell powershell = PowerShell.Create(); 

      PSCommand command = new PSCommand(); 
      command.AddCommand("New-PSSession"); 
      command.AddParameter("ConfigurationName", "Microsoft.Exchange"); 
      command.AddParameter("ConnectionUri", new Uri(connectionUri)); 
      command.AddParameter("Credential", credential); 
      command.AddParameter("Authentication", "Basic"); 
     // command.AddCommand("Set-ExecutionPolicy RemoteSigned"); 
      powershell.Commands = command; 


      runspace.Open(); 
      powershell.Runspace = runspace; 
      Collection<System.Management.Automation.PSObject> 
       result = powershell.Invoke(); 

      if (powershell.Streams.Error.Count > 0 || result.Count != 1) 
      { 

       throw new Exception("failed"); 
      } 


       powershell = PowerShell.Create(); 
       command = new PSCommand(); 
       command.AddCommand("Invoke-Command"); 

       const String ScriptBlock = "Get-User {0} | Enable-RemoteMailbox -RemoteRoutingAddress {1};"; 
       String ScriptBlockstr = string.Format(ScriptBlock, GetUser, MailboxUser); 

       command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create(ScriptBlockstr)); 
       command.AddParameter("Session", result[0]); 

       powershell.Commands = command; 
       powershell.Runspace = runspace; 
       var mailBoxes = powershell.Invoke(); 

回答

0

由於我們不知道MS Exchange服務器的配置和應用程序如何觸發它不是那麼容易解決這個問題。首先需要注意的是,根據您的配置,您可以使用端口5985(http WinRM),5986(https WinRM)或443或80(如Microsoft here所解釋)或您可能已配置的任何端口。當您使用New-PsSession並使用ComputerName 5985/5986時。如果使用ConnectionURI,則使用端口80或443(有關更多信息,請參閱here)。

用在你的心中現在這裏有一些基本的故障排除步驟檢查:

  1. 檢查,如果你使用的是HTTPS或HTTP作爲ConnectionUri,如果它們之間,如果一個差按預期工作
  2. 最好的方法是使用https(通過ComputerName或ConnectionURI)消除https被強制的任何「安全實現」問題,然後在您嘗試繞過https時中斷。要爲WinRM配置HTTPS(通過5986)使用(檢查here有詳細介紹):

winrm quickconfig -transport:https

因爲如果使用WinRM和如果HTTPS不是傳輸,則目標遠程計算機必須被配置可信任的主機列表(請參閱以下內容,有關更多信息,請參閱here)。

  1. 如果您使用https(通過ComputerName或ConnectionURI),請確保運行解決方案的服務器正在信任該連接。如果您使用的是ConnectionURI,請在非Exchange服務器的瀏覽器中輸入URL並檢查是否有任何SSL證書問題。確保您在這裏使用完整的合格域名。
  2. 如果您在非Exchange Server上配置了代理,請確保您的連接繞過代理(請參閱here),以避免嘗試通過代理完成連接,但這不是您想要的方式。
  3. 檢查通過telnet如果從受影響的服務器對你的MS Exchange服務器的端口是開放的(參見端口信息在從發佈頂部)
  4. 根據您的配置,您可能需要使用不同的認證(如談判,基本,Kerberos,...)。確保您在這裏使用正確的身份驗證來處理您的情況。請注意,Kerberos只能在域上下文中使用,這意味着非Exchange服務器必須添加到相同的ActiveDirectory域!要測試連接,您可以使用powershell(通過以不同用戶身份運行),並且在連接檢查期間您也可能會繞過一些SSL驗證檢查(SkipCACheck,SkipCNCheck,SkipRevocationCheck)。請參閱下面的一些例子(更多信息的here)還要檢查HTTP和HTTPS選項:

$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<FQDN of Exchange Mailbox server>/PowerShell/ -Authentication Negotiate -Credential $UserCredential -SkipCACheck -SkipCNCheck -SkipRevocationCheck Import-PSSession $Session

或者,如果你知道哪種認證應使用用途:

$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<FQDN of Exchange Mailbox server>/PowerShell/ -Authentication Kerberos -Credential $UserCredential -SkipCACheck -SkipCNCheck -SkipRevocationCheck Import-PSSession $Session

$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://<FQDN of Exchange Mailbox server>/PowerShell/ -Authentication Basic -Credential $UserCredential -SkipCACheck -SkipCNCheck -SkipRevocationCheck Import-PSSession $Session

  1. 是否爲您正在使用的taskuser啓用了遠程powershell?

Set-User YourTaskUser -RemotePowerShellEnabled $True

  • 確保按要求你和(例如被配置爲在預期的認證方法)的MS交換的powershell目錄構造。首先:
  • Get-PowerShellVirtualDirectory "Exchange2010\PowerShell (Default Web Site)"

  • 在大多數情況下,你將能夠與其他域中的遠程計算機的工作。但是,如果遠程計算機不在受信任的域中,遠程計算機可能無法驗證您的憑據。要啓用身份驗證,您需要將遠程計算機添加到WinRM中本地計算機的可信主機列表(請參閱here)。要做到這一點,類型:

    winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'

  • 檢查認證(=基本)被改變,或者如果AllowUnencrypted設置爲true。這兩個都不是默認設置,如果這樣做可能會導致意外問題(除了限制安全性)。

  • 您還可以使用Test-WSMan來檢查基本和/或kerberos身份驗證是否按預期工作(通過http或https和您配置/使用的WinRM端口)。下面是一些例子:

  • Test-WSMan -ComputerName https://server2008:5986 -Auth basic -Cred B\MY_USER_NAME

    和/或

    Test-WSMan -ComputerName https://server2008:5986 -Auth kerberos

  • 當您使用WinRM的確保您的taskuser添加的到本地操作系統WinRMRemoteWMIUsers組,因爲默認WinRM僅限於該組中的用戶或本地管理組中的用戶(請參閱here)。
  • +0

    我們可以在調試模式下從powershell控制檯以及從Visual Studio運行命令......當相同的代碼部署在IIS上的相同服務器上時,它會引發訪問被拒絕。 –

    +0

    任何想法可能會出錯? –

    +0

    您是否檢查過上述選項?你沒有說明你的代碼中是否使用了https或http,所以我在黑暗中完全絆倒了,而你給我的信息太少了......你只重複你的代碼而不檢查我的答案...... – BastianW

    相關問題