Powershell远程控制-策略不允许委派用户凭据

33

我是powershell的新手,我在使用凭据委派时遇到了困难。我有以下脚本:

$session = New-PSSession myserver -Authentication CredSSP -Credential DOMAIN\Administrator
Invoke-Command -Session $session -ScriptBlock { <Some PowerShell Command> }

在运行之前,我做了以下操作:

  1. 在我的服务器上运行Enable-PSRemoting
  2. 在我的服务器上运行Enable-WSManCredSSP Server
  3. 在我的服务器上运行Restart-Service WinRM
  4. 在客户端上运行Enable-WSManCredSSP Client –DelegateComputer myserver
  5. 重新启动了服务器和客户端。

但是一旦运行脚本,我就会收到以下错误信息:

[myserver] Connecting to remote server failed with the following error message : The WinRM client cannot process the request. A computer policy does not allow the delegation of
 the user credentials to the target computer. Use gpedit.msc and look at the following policy: Computer Configuration -> Administrative Templates -> System -> Credentials Delega
tion -> Allow Delegating Fresh Credentials.  Verify that it is enabled and configured with an SPN appropriate for the target computer. For example, for a target computer name "m
yserver.domain.com", the SPN can be one of the following: WSMAN/myserver.domain.com or WSMAN/*.domain.com. For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionOpenFailed

我已经按照错误信息中提到的政策进行了检查,但一切似乎都没问题。还有什么可能会阻止我吗?


你在远程服务器上被列为管理员了吗? - BlackHatSamurai
@Blaine:我正在尝试模拟的用户DOMAIN\Administrator是远程服务器的管理员。我还使用管理帐户连接到客户端机器。 - ChrisB
你是否在远程计算机上启用了远程脚本:Enable-PSRemoting - BlackHatSamurai
我已经编辑了问题并提到了这一点。感谢您指出。 - ChrisB
4个回答

34

在服务器上执行以下操作:

Enable-WSManCredSSP -Role Server

在客户端上执行以下操作:

set-item wsman:localhost\client\trustedhosts -value *

Enable-WSManCredSSP -Role Client –DelegateComputer *

在客户端上使用gpedit.msc启用向WSMAN/*委派新凭据的功能:

  1. 展开本地计算机策略,展开计算机配置,展开管理模板,展开系统,然后单击凭据委派
  2. 设置窗格中,双击仅使用NTLM服务器身份验证允许委派新凭据
  3. 仅使用NTLM服务器身份验证允许委派新凭据对话框中,执行以下操作:
  4. 单击已启用
  5. 选项区域中,单击显示
  6. 在值中,键入WSMAN/*,然后单击确定。确保选择了将操作系统默认值与上述输入连接起来,然后单击确定

现在以下命令可以正常工作(在输入密码后):

Invoke-Command { dir \\fileserver\devtools } -computer appserver01 -authentication credssp -credential domain\user

请查看MSDN论坛

请查看TechNet


你能澄清一下“使用gpedit.msc启用将新的凭据委派给WSMAN / *”吗? - Peter
1
当然,@Peter。我刚刚添加了更多的指示。我还添加了TechNet参考。 - Akira Yamamoto
2
只是为了帮助其他人:在客户端计算机上运行gpedit的步骤。 - Mário Meyrelles
您还可以使用以下命令将凭据永久添加:cmdkey /add:"SERVER" /user:"DOMAIN\USERNAME" /pass:"PASSWORD" - maeneak
1
这绝对是答案!非常感谢你,伙计 :) - Danilo Carrabino
显示剩余2条评论

16

多亏了这个页面我终于让它工作了。它提供了一个脚本,通过直接设置适当的注册表键来设置所需的凭据委派策略。一旦我以管理员权限运行了那个脚本,我就能成功地建立与myserver的CredSSP连接:

Enable-WSManCredSSP -Role client -DelegateComputer *.mydomain.com

$allowed = @('WSMAN/*.mydomain.com')

$key = 'hklm:\SOFTWARE\Policies\Microsoft\Windows\CredentialsDelegation'
if (!(Test-Path $key)) {
    md $key
}
New-ItemProperty -Path $key -Name AllowFreshCredentials -Value 1 -PropertyType Dword -Force            

$key = Join-Path $key 'AllowFreshCredentials'
if (!(Test-Path $key)) {
    md $key
}
$i = 1
$allowed |% {
    # Script does not take into account existing entries in this key
    New-ItemProperty -Path $key -Name $i -Value $_ -PropertyType String -Force
    $i++
}

3
这是从客户端还是服务器运行的? - Gina Marano
来自客户端。 - Mário Meyrelles

11

我需要完全自动化我的解决方案,特别是解决方案中涉及进入GPO编辑器的部分。

1)启用远程PowerShell

Enable-PSRemoting -force

2) 启用 CredSSP

Enable-WSManCredSSP -Role Server -Force
Enable-WSManCredSSP -Role Client -DelegateComputer locahost -Force
Enable-WSManCredSSP -Role Client -DelegateComputer $env:COMPUTERNAME -Force
Enable-WSManCredSSP -Role Client -DelegateComputer $domain -Force
Enable-WSManCredSSP -Role Client -DelegateComputer "*.$domain" -Force
Set-Item -Path "wsman:\localhost\service\auth\credSSP" -Value $True -Force

3) 通过注册表启用 NTLM 新凭据:

New-Item -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\CredentialsDelegation -Name AllowFreshCredentialsWhenNTLMOnly -Force
New-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\CredentialsDelegation\AllowFreshCredentialsWhenNTLMOnly -Name 1 -Value * -PropertyType String

只有在这之后,我才能够作为本地管理员启动powershell脚本,并且能够在PSSession中运行并执行AD操作。

$secpasswd = ConvertTo-SecureString $adPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("$domain\Admin", $secpasswd)
$adminSession = New-PSSession -Credential $credential -Authentication Credssp;

$sb = {
  param($p1, $p2)

  whoami

  New-ADUser ....
}

Invoke-Command -Session $adminSession -Script $sb -ArgumentList $domain,$userPassword

1
在执行脚本时,我遇到了一些愚蠢的问题,在事后看来似乎是显而易见的...就是我把所有的脚本都放在一个文件中,在远程机器上一次性执行。显然,你第一次会话的权限没有更新以反映你刚刚更改的内容,所以要更改设置,关闭会话,然后重新开始新的会话以获取这些设置......这对我来说一开始并不明显。 - Chris

5

对于Akira在上面所提供的答案,我需要在gpedit.msc中设置"Allow Delegating Fresh Credentials with NTLM-only Server Authentication"而不是"Allow Delegating Fresh Credentials"。


谢谢,我刚刚修正了我的回答。 - Akira Yamamoto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接