0

解决 SSL 证书问题后,我得到一个奇怪的异常。请帮忙!我的代码: PSCredential credential = new PSCredential("domain\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);

    }

公共集合 GetUserInformation(int count, Runspace runspace) { using (PowerShell powershell = PowerShell.Create()) {

        powershell.AddCommand("Get-Users");
        powershell.AddParameter("ResultSize", count);

        runspace.Open();//**error happens**

        powershell.Runspace = runspace;

        return powershell.Invoke();
    }
}

错误消息: “连接到远程服务器失败并显示以下错误消息:WinRM 客户端无法处理请求。WinRM 客户端尝试使用协商身份验证机制,但目标计算机 (www.xxx.com:443) 返回了“访问权限” denied' 错误。更改配置以允许使用协商身份验证机制或指定服务器支持的身份验证机制之一。要使用 Kerberos,请将本地计算机名称指定为远程目标。同时验证客户端计算机和目标计算机已加入域。要使用 Basic,请将本地计算机名称指定为远程目标,指定 Basic 身份验证并提供用户名和密码。

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

4

3 回答 3

3

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

如果这不能解决问题,请确保在 PowerShell 网站上启用了基本身份验证。

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

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

于 2011-07-27T16:38:50.300 回答
1

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

有关详细信息,请参阅http://technet.microsoft.com/en-us/library/dd351136.aspxhttp://technet.microsoft.com/en-us/library/dd347642.aspx

于 2011-07-26T23:15:48.433 回答
0

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

  • 在客户端和服务器上将 ExecutionPolicy 设置为不受限制
  • 正确配置客户端和服务器上的 TrustedHosts
  • 在客户端和服务器上启用基本身份验证
  • 确保在 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 
}
于 2013-06-25T14:51:28.047 回答