2017-08-20 39 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

    相关问题