2011-07-26 37 views
0

解决了SSL证书问题后,我收到了一个奇怪的异常。请帮忙! 我的代码: PSCredential credential = new PSCredential(“domain \ administrator”,securePwd);从c调用Exchange命令行管理程序时出现连接错误#

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(诠释计数,运行空间运行空间) { 使用(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)返回了“访问被拒绝”错误。允许使用协商身份验证机制的配置或指定服务器支持的身份验证机制之一。要使用Kerberos,请将本地计算机名称指定为远程目标。还要验证客户端计算机和目标计算机是否已加入域。要使用Basic,请指定本地计算机名称作为远程目标,指定基本身份验证并提供用户名和密码。“

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

回答

3

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

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

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

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

+0

非常感谢你!但是当我按照你的建议做了什么后,出现了一个新的错误,表示“访问被拒绝”。任何想法? –

+0

没关系,我只是从证书中删除域部分,现在它得到新的错误:“术语'Get-Users'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。”但是,身份验证问题我认为已经解决了。再次感谢你。 –

+0

这似乎不是Exchange PowerShell命令。要查看所有可用的命令,请运行Exchange命令行管理程序并键入get-command。 – Pedro

0

我可以总结的步骤,以使基本身份验证工作,甚至从外域的计算机:在客户端和服务器

  • 配置正确的客户端和服务器
  • TrustedHosts

    • 的Set-ExecutionPolicy以无限制
    • 启用客户端和服务器上的基本身份验证
    • 确保基本身份验证角色安装在Web服务器(IIS)的安全下
    • en支持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 
    } 
    
    相关问题