【问题标题】:Enable mail box from C# powershell从 C# powershell 启用邮箱
【发布时间】:2018-01-28 09:47:56
【问题描述】:

为本地交换服务器启用邮箱的代码在 Visual Studio 以调试模式运行时有效,但在同一实例上的 IIS 上部署时失败。命令也适用于 Powershell 控制台 它抛出异常无法连接到服务器访问被拒绝。

有人可以帮我解决这个问题吗?

PFB sn-p 在 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();

【问题讨论】:

    标签: c# visual-studio powershell iis exchange-server


    【解决方案1】:

    由于我们不知道 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. 根据您的配置,您可能需要使用不同的身份验证(例如 Negotiate、Basic、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. 是否为您正在使用的任务用户启用了远程 powershell?

    Set-User YourTaskUser -RemotePowerShellEnabled $True

    1. 确保根据您的需要配置 MS Exchange powershell 目录并且(例如配置了预期的身份验证方法)。开始:

    Get-PowerShellVirtualDirectory "Exchange2010\PowerShell (Default Web Site)"

    1. 在大多数情况下,您将能够使用其他域中的远程计算机。但是,如果远程计算机不在受信任的域中,则远程计算机可能无法验证您的凭据。要启用身份验证,您需要将远程计算机添加到 WinRM 中本地计算机的受信任主机列表(请参阅here)。为此,请键入:

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

    2. 检查身份验证(= 基本)是否已更改,或者 AllowUnencrypted 是否设置为 true。两者都不是默认设置,如果这样做可能会导致意外问题(除了它限制安全性的事实之外)。

    3. 您还可以使用 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

    1. 当您使用 WinRM 时,请确保将您的任务用户添加到本地操作系统 WinRMRemoteWMIUsers 组,因为默认情况下 WinRM 仅限于该组中的用户或本地管理组中的用户(请参阅here)。

    【讨论】:

    • 我们能够在调试模式下从 powershell 控制台和 Visual Studio 运行命令...当相同的代码部署在 IIS 上的同一台服务器上时,它会抛出访问被拒绝。
    • 您是否检查了上述选项?您没有指定在代码中使用 https 还是 http,所以我完全在黑暗中跌跌撞撞,您给我的信息如此之少...您只重复您的代码而不检查我的答案...
    • 仅供参考,我确实检查了您的答案,这是对您的回复,表明它在 powershell 控制台中针对您提出的问题工作。并且 authn 是 Basic ,这是从 powershell console 、 VS 和 IIS 运行时设置的。连接 uri 是 http 而不是 https
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2017-04-30
    • 2016-08-24
    • 1970-01-01
    • 2012-01-11
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多