【问题标题】:Powershell remoting - Policy does not allow the delegation of user credentialsPowershell 远程处理 - 策略不允许委派用户凭据
【发布时间】:2013-08-09 10:27:08
【问题描述】:

我是 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

我检查了错误消息中提到的政策,但一切似乎都很好。还有什么可能阻止我?

【问题讨论】:

  • 您在远程机器上被列为管理员吗?
  • @Blaine:我试图模拟的用户 DOMAIN\Administrator 是远程服务器的管理员。我还使用管理帐户连接到客户端计算机。
  • 您是否在远程机器上启用了远程脚本:Enable-PSRemoting
  • 我有。刚刚编辑了问题以提及这一点。感谢您指出。

标签: powershell powershell-remoting winrm


【解决方案1】:

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

Enable-WSManCredSSP -Role Server

在客户端执行以下操作:

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

Enable-WSManCredSSP -Role Client –DelegateComputer *

在客户端上使用gpedit.msc 启用将新凭据委托给 WSMAN/*:

  1. 展开Local Computer Policy,展开Computer Configuration,展开 Administrative Templates,展开System,然后点击Credential Delegation
  2. Settings 窗格中,双击Allow Delegating Fresh Credentials with NTLM-only Server Authentication
  3. Allow Delegating Fresh Credentials with NTLM-only Server Authentication 对话框中,执行以下操作:
  4. 点击Enabled
  5. Options区域,点击Show
  6. 在值中,键入WSMAN/*,然后单击OK。确保 Concatenate OS defaults with input above 被选中,然后 点击OK

现在可以使用以下命令(在提示密码后):

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

MSDN forums

TechNet

【讨论】:

  • 您能否澄清“使用 gpedit.msc 启用将新凭据委托给 WSMAN/*”?
  • 当然@Peter。我只是添加了更多说明。我还添加了 TechNet 参考。
  • 只是为了帮助别人:在客户端计算机上运行 gpedit 的步骤。
  • 您也可以使用以下方式永久添加凭据:cmdkey /add:"SERVER" /user:"DOMAIN\USERNAME" /pass:"PASSWORD"
  • 这绝对是答案!非常感谢你:)
【解决方案2】:

感谢this page,我终于让它工作了。它提供了一个脚本,通过直接设置适当的注册表项来设置所需的凭据委派策略。一旦我以管理员权限运行该脚本,我就能够成功地建立与 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】:

我不得不完全自动化我的解决方案,尤其是解决方案中让您进入 GPO 编辑器的部分部分。

1) 启用远程 PS

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

只有在此之后,我才能以能够在 PSSession 中运行并执行 AD 操作的本地管理员身份启动 powershell 脚本。

$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-script 的脚本。简单地说,这是行不通的。显然,您的第一个会话的权限不会更新以反映您刚刚更改的内容,因此更改设置,关闭您的会话,然后开始一个新的会话以获取这些设置......起初对我来说并不明显.
【解决方案4】:

根据 Akira 的上述回答,在 gpedit.msc 中,我必须设置“允许使用仅限 NTLM 的服务器身份验证委派新凭据”而不是“允许委派新凭据”。

【讨论】:

  • 谢谢,我刚刚修正了我的答案。
猜你喜欢
  • 2010-12-14
  • 1970-01-01
  • 2013-08-05
  • 2016-07-13
  • 2020-10-02
  • 2019-10-30
  • 1970-01-01
  • 2014-07-05
  • 2021-12-18
相关资源
最近更新 更多