如何使用 PowerShell 在远程计算机上安装证书?
How can I install a certificate on a remote machine with PowerShell?
我正在尝试使用 PowerShell 安装证书。我正在通过使用自己的计算机创建远程会话来测试安装。我知道该命令是正确的,因为它在远程会话之外成功安装了证书。当我对自己的计算机进行远程会话时,我收到错误消息:
Access is denied. 0x80070005 (WIN32: 5 ERROR_ACCESS_DENIED). This may be the result of user credentials being required on the remote machine. See
Enable-WSManCredSSP Cmdlet help on how to enable and use CredSSP for delegation with PowerShell remoting.
如果我 运行 这个命令没有远程会话它工作:
Import-PfxCertificate -FilePath "\network\storage\site.com.pfx" -CertStoreLocation "Cert:\LocalMachine\WebHosting" -Password (ConvertTo-SecureString -String "foobar" -AsPlainText -Force)
当我使用远程会话执行命令(通过 Invoke-Command
)时,出现上述错误:
$session = New-PSSession $Env:ComputerName
Invoke-Command -Session $session -ScriptBlock { Import-PfxCertificate -FilePath "\network\storage\site.com.pfx" -CertStoreLocation "Cert:\LocalMachine\WebHosting" -Password (ConvertTo-SecureString -String "foobar" -AsPlainText -Force) }
如何解决此权限问题?
您遇到所谓的双跳身份验证问题。如果使用普通身份验证,您将无法从您调用命令的计算机向第二台计算机进行身份验证。
要解决这个问题,您可以使用 CredSSP。
要在被调用的机器上启用 CredSSP:
Enable-WSManCredSSP -Role Server -force
要在客户端启用 CredSSP:
Enable-WSManCredSSP -Role Client -DelegateComputer server.domain.com -force
-delegateComputer
参数需要 FQDN,但也需要通配符。
启用 CredSSP 后,您可以使用它通过参数 -authentication CredSSP
调用您的命令
我花了很多时间试图解决权限问题,最后转向了不同的方向。在修改权限的时候有一个flaw\bug。无法记住所有详细信息,但 ACL 属性会尝试访问您尝试更改的密钥的所有权。这可能不是每个人的理想选择,但它适用于将证书部署到我们所有的 NLA RDP 系统。
Set-Location "c:\windows\system32"
#if certificate is missing, will install wild card certificate into the store
icacls 'C:\ProgramData\Microsoft\Crypto\Keys' /save C:\ProgramData\Microsoft\Crypto\Keys\ACLfile_for_Keys_Dir /T
takeown /f C:\ProgramData\Microsoft\Crypto\Keys\*.*
icacls 'C:\ProgramData\Microsoft\Crypto\Keys\*.*' /grant 'Network Service:(R)'
#icacls C:\ProgramData\Microsoft\Crypto /restore C:\ProgramData\Microsoft\Crypto\Keys\ACLfile_for_Keys_Dir #(restores original ACL for recovery)
icacls 'C:\ProgramData\Microsoft\Crypto\keys' /setowner system
icacls 'C:\ProgramData\Microsoft\Crypto\keys\*.*' /setowner system
注释掉的两行是我的退出计划,以防出现问题。
我正在尝试使用 PowerShell 安装证书。我正在通过使用自己的计算机创建远程会话来测试安装。我知道该命令是正确的,因为它在远程会话之外成功安装了证书。当我对自己的计算机进行远程会话时,我收到错误消息:
Access is denied. 0x80070005 (WIN32: 5 ERROR_ACCESS_DENIED). This may be the result of user credentials being required on the remote machine. See Enable-WSManCredSSP Cmdlet help on how to enable and use CredSSP for delegation with PowerShell remoting.
如果我 运行 这个命令没有远程会话它工作:
Import-PfxCertificate -FilePath "\network\storage\site.com.pfx" -CertStoreLocation "Cert:\LocalMachine\WebHosting" -Password (ConvertTo-SecureString -String "foobar" -AsPlainText -Force)
当我使用远程会话执行命令(通过 Invoke-Command
)时,出现上述错误:
$session = New-PSSession $Env:ComputerName
Invoke-Command -Session $session -ScriptBlock { Import-PfxCertificate -FilePath "\network\storage\site.com.pfx" -CertStoreLocation "Cert:\LocalMachine\WebHosting" -Password (ConvertTo-SecureString -String "foobar" -AsPlainText -Force) }
如何解决此权限问题?
您遇到所谓的双跳身份验证问题。如果使用普通身份验证,您将无法从您调用命令的计算机向第二台计算机进行身份验证。
要解决这个问题,您可以使用 CredSSP。
要在被调用的机器上启用 CredSSP:
Enable-WSManCredSSP -Role Server -force
要在客户端启用 CredSSP:
Enable-WSManCredSSP -Role Client -DelegateComputer server.domain.com -force
-delegateComputer
参数需要 FQDN,但也需要通配符。
启用 CredSSP 后,您可以使用它通过参数 -authentication CredSSP
我花了很多时间试图解决权限问题,最后转向了不同的方向。在修改权限的时候有一个flaw\bug。无法记住所有详细信息,但 ACL 属性会尝试访问您尝试更改的密钥的所有权。这可能不是每个人的理想选择,但它适用于将证书部署到我们所有的 NLA RDP 系统。
Set-Location "c:\windows\system32"
#if certificate is missing, will install wild card certificate into the store
icacls 'C:\ProgramData\Microsoft\Crypto\Keys' /save C:\ProgramData\Microsoft\Crypto\Keys\ACLfile_for_Keys_Dir /T
takeown /f C:\ProgramData\Microsoft\Crypto\Keys\*.*
icacls 'C:\ProgramData\Microsoft\Crypto\Keys\*.*' /grant 'Network Service:(R)'
#icacls C:\ProgramData\Microsoft\Crypto /restore C:\ProgramData\Microsoft\Crypto\Keys\ACLfile_for_Keys_Dir #(restores original ACL for recovery)
icacls 'C:\ProgramData\Microsoft\Crypto\keys' /setowner system
icacls 'C:\ProgramData\Microsoft\Crypto\keys\*.*' /setowner system
注释掉的两行是我的退出计划,以防出现问题。