C# 从c调用Exchange命令行管理程序时出现连接错误

C# 从c调用Exchange命令行管理程序时出现连接错误,c#,cmdlets,C#,Cmdlets,在解决SSL证书问题后,我遇到了一个奇怪的异常。请帮忙! 我的代码: PSCredential credential=新PSCredentialdomain\administrator,securePwd WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://www.xxx.com/powershell"), "http://schemas.microsoft.com/powershel

在解决SSL证书问题后,我遇到了一个奇怪的异常。请帮忙! 我的代码: PSCredential credential=新PSCredentialdomain\administrator,securePwd

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://www.xxx.com/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
    Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
    using (runspace)
    {
        Collection<PSObject> psObject = GetUserInformation(10, runspace);

    }
错误消息: 连接到远程服务器失败,错误消息如下:WinRM客户端无法处理该请求。WinRM客户端尝试使用协商身份验证机制,但目标计算机www.xxx.com:443返回“拒绝访问”错误。更改配置以允许使用协商身份验证机制,或指定服务器支持的身份验证机制之一。要使用Kerberos,请指定本地计算机名作为远程目标。还要验证客户端计算机和目标计算机是否已加入域。要使用Basic,请指定本地计算机名作为远程目标,指定基本身份验证并提供用户名和密码


我使用基本身份验证,并提供用户名和凭据,为什么它说尝试使用协商身份验证机制?

在这种情况下不允许使用基本身份验证,除非在服务器上明确配置。。。您可以在服务器端启用它或使用Kerberos/NTLM


有关详细信息,请参阅和

除非在服务器上明确配置,否则在此场景中不允许使用基本身份验证。。。您可以在服务器端启用它或使用Kerberos/NTLM


有关详细信息,请参见和

首先,在创建运行空间之前,尝试设置connectionInfo.AuthenticationMechanism属性。因此,交换第一个代码段中第2行和第3行的顺序

如果无法解决此问题,请确保在PowerShell网站上启用了基本身份验证

要执行此操作,您需要转到IIS管理器、站点、默认网站、PowerShell,选择身份验证功能,然后启用基本身份验证


如果“身份验证”功能页上没有基本身份验证选项,则需要通过转到服务器管理器来安装它,选择Web服务器角色,例如添加角色服务,然后在树视图的安全节点下选择基本身份验证。

首先,在创建运行空间之前,请尝试设置connectionInfo.AuthenticationMechanism属性。因此,交换第一个代码段中第2行和第3行的顺序

如果无法解决此问题,请确保在PowerShell网站上启用了基本身份验证

要执行此操作,您需要转到IIS管理器、站点、默认网站、PowerShell,选择身份验证功能,然后启用基本身份验证


如果“身份验证”功能页上没有基本身份验证选项,则需要通过转到服务器管理器来安装它,在树视图的安全节点下选择Web服务器角色,例如添加角色服务,选择基本身份验证。

我可以总结使基本身份验证即使在域外的计算机上也能工作的步骤:

在客户端和服务器上将ExecutionPolicy设置为Unrestricted 在客户端和服务器上正确配置受信任的主机 在客户端和服务器上启用基本身份验证 确保在Web服务器IIS的安全性下安装了基本身份验证角色 为PowerShell虚拟目录启用基本身份验证 使用HTTP而不是https访问服务器。 以下是工作代码:

PowerShell powershell = PowerShell.Create();
String pass = "password";
SecureString passSecure = new SecureString();
foreach (char c in pass.ToCharArray())
{
    passSecure.AppendChar(c);
}
PSCredential cred = new PSCredential("user", passSecure);

string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
Uri connectTo = new Uri("http://192.168.69.116/powershell/");            
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo, schemaURI, cred);
connectionInfo.MaximumConnectionRedirectionCount = 5;
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
//connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;
connectionInfo.SkipRevocationCheck = true;
Runspace remoteRunspace=null;
try
{
   remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
   remoteRunspace.Open();
}
catch (Exception err)
{
    //Handle error 
}

我可以总结使基本身份验证即使在域外的计算机上也能工作的步骤:

在客户端和服务器上将ExecutionPolicy设置为Unrestricted 在客户端和服务器上正确配置受信任的主机 在客户端和服务器上启用基本身份验证 确保在Web服务器IIS的安全性下安装了基本身份验证角色 为PowerShell虚拟目录启用基本身份验证 使用HTTP而不是https访问服务器。 以下是工作代码:

PowerShell powershell = PowerShell.Create();
String pass = "password";
SecureString passSecure = new SecureString();
foreach (char c in pass.ToCharArray())
{
    passSecure.AppendChar(c);
}
PSCredential cred = new PSCredential("user", passSecure);

string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
Uri connectTo = new Uri("http://192.168.69.116/powershell/");            
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo, schemaURI, cred);
connectionInfo.MaximumConnectionRedirectionCount = 5;
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
//connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;
connectionInfo.SkipRevocationCheck = true;
Runspace remoteRunspace=null;
try
{
   remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
   remoteRunspace.Open();
}
catch (Exception err)
{
    //Handle error 
}

非常感谢你!但在我按照你的建议做了之后,出现了一个新的错误,即访问被拒绝。有什么想法吗?没关系,我只是从凭据中删除了域部分,现在它出现了新的错误:术语“Get Users”不被识别为cmdlet、函数、脚本文件或可操作程序的名称。但我认为认证问题已经解决了。再次感谢。这似乎不是Exchange PowerShell命令。要查看所有可用命令,请运行Exchange命令行管理程序并键入get-command。非常感谢!但在我按照你的建议做了之后,出现了一个新的错误,即访问被拒绝。有什么想法吗?没关系,我只是从凭据中删除了域部分,现在它出现了新的错误:术语“Get Users”不被识别为cmdlet、函数、脚本文件或可操作程序的名称。但我认为认证问题已经解决了。再次感谢。这似乎不是Exchange PowerShell命令。要查看所有可用命令,请运行Exchange命令行管理程序并键入get命令。