![](/img/trans.png)
[英]PowerShell HTTPS GET using client certificate from certstore
[英]How do I import a certificate to the Certstore on remote systems when WinRM is disabled with powershell
当由于安全原因在环境中禁用WinRM时,如何使用Powershell将证书导入远程系统上的Certstore。 我尝试了一些解决方法,但都失败了。 请轻松一点,我是新手。
我已经尝试过这种方法,是的,我知道它是不受信任的商店。
$cert = getchilditem -path "SharePath.cer"
$server = Get-content ".\servers.txt"
$server | foreach { $cert | import-certificate -CertStoreLocation Cert:LocalMachine\Disallowed
通过组策略最好将证书部署到计算机,因为它是一个更健壮且易于使用的过程。
话虽这么说,如果您不能使用组策略,则可以根据目标系统的OS使用其他几种解决方案之一。
Boe Prox编写了一个Import-Certificate功能和说明,其幕后使用.Net允许在没有WinRM或Windows 10或更早版本的远程计算机上进行导入。使用他的解决方案,您可以执行以下操作:
$File = "C:\temp\SomeRootCA.cer"
$Computername = 'Server1','Server2','Client1','Client2'
Import-Certificate -Certificate $File -StoreName Disallowed -StoreLocation LocalMachine -ComputerName $Computername
如果您使用的是Windows 10,则可以使用内置的Import-Certificate,但这仅适用于本地系统。 因此,您需要使用Invoke-Command(需要WinRM)包装它,也可以回退到Invoke-WMIMethod 。 使用或使用输出并不是那么友好,但是它将起作用。 这是在没有WinRM的远程计算机上执行功能的有效示例。 对于复杂的命令,您将需要使用powershell.exe的-EncodedCommand参数 。
将证书复制到远程系统,以避免2跳问题
Copy-Item "C:\temp\SomeRootCa.cer" "\\Computer1\C$\temp\"
编码我们要使用的命令
$encoded = [Convert]::ToBase64String(
[System.Text.Encoding]::Unicode.GetBytes(
{Import-Certificate -FilePath C:\temp\somerootca.cer -CertStoreLocation cert:\localmachine\disallowed}
)
)
在远程计算机上生成进程
Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList "powershell.exe -encodedcommand $encoded" -ComputerName Computer1
您将在计算机上获得类似于以下内容的输出
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId : 8748
ReturnValue : 0
PSComputerName :
请注意,返回值0表示成功,仅表示生成了powershell,而不表示您执行的命令成功。 如果您需要一些日志记录,则需要将其烘焙到您编码的命令中
Get-Content C:\some\file\name\here.txt | Foreach-Object -begin {
$encoded = [Convert]::ToBase64String(
[System.Text.Encoding]::Unicode.GetBytes(
{Import-Certificate -FilePath C:\temp\somerootca.cer -CertStoreLocation cert:\localmachine\disallowed}
)
)
} -process {
if(Test-Connection $_ -quiet){
Copy-Item "C:\temp\SomeRootCa.cer" "\\$_\C$\temp\"
Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList "powershell.exe -encodedcommand $encoded" -ComputerName $_
}
}
请不要将其复制/粘贴到生产环境中。 阅读并了解正在发生的事情。 使用ISE调试功能的各个部分,并将其一点一点地组合在一起。
还要注意,如果您不做最少的研究(即Get-Content和Foreach-Object)并向SSCCE显示您的问题很可能被标记为脱题并关闭,则SO不是代码编写服务 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.