使用 PowerShell 从 Windows 7 远程访问 Exchange Server 2013

使用 PowerShell 从 Windows 7 远程访问 Exchange Server 2013

我正在尝试使用 Windows 7 中的 PowerShell 远程访问 Exchange Server 2013。下面是我正在使用的代码片段:

public static void ConnectToPowerShell()
{
        string password = "password";
        string userName = "Administrator";

        System.Uri uri = new Uri(@"http://ExchangeServer/powershell?serializationLevel=Full");
        System.Security.SecureString securePassword = String2SecureString(password);

        System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(userName, securePassword);

        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", uri);
        command.AddParameter("Credential", creds);
        command.AddParameter("Authentication", "Default");
        PSSessionOption sessionOption = new PSSessionOption();
        sessionOption.SkipCACheck = true;
        sessionOption.SkipCNCheck = true;
        sessionOption.SkipRevocationCheck = true;
        command.AddParameter("SessionOption", sessionOption);

        powershell.Commands = command;

        try
        {
            // open the remote runspace
            runspace.Open();

            // associate the runspace with powershell
            powershell.Runspace = runspace;

            // invoke the powershell to obtain the results
            Collection<PSSession> result = powershell.Invoke<PSSession>();
            String str=null;
            foreach (ErrorRecord current in powershell.Streams.Error)
            {
                // Console.WriteLine("The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
                // " | InnerException: " + current.Exception.InnerException);
                str = "The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
                   " | InnerException: " + current.Exception.InnerException;
                MessageBox.Show(str);
            }
            if (result.Count != 1)
                throw new Exception("Unexpected number of Remote Runspace connections returned.");

            // Set the runspace as a local variable on the runspace
            powershell = PowerShell.Create();
...........................................
...........................................
}

在该行:

Collection<PSSession> result = powershell.Invoke<PSSession>();

它给出了以下异常:

打开远程运行空间时发生以下错误:System.Management.Automation.Remoting.PSRemotingTransportException:连接到远程服务器失败,并显示以下错误消息:WinRM 客户端无法处理请求。WinRM 客户端尝试使用协商身份验证机制,但目标计算机 (rmex13:80) 返回“拒绝访问”错误。更改配置以允许使用协商身份验证机制或指定服务器支持的身份验证机制之一。要使用 Kerberos,请将本地计算机名称指定为远程目标。还要验证客户端计算机和目标计算机是否已加入域。要使用 Basic,请将本地计算机名称指定为远程目标,指定基本身份验证并提供用户名和密码。服务器报告的可能的身份验证机制:有关更多信息,请参阅 about_Remote_Troubleshooting 帮助主题

我该如何修复此类异常?

相关内容