簡體   English   中英

使用Powershell禁用WinRM時如何將證書導入遠程系統上的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,而不表示您執行的命令成功。 如果您需要一些日志記錄,則需要將其烘焙到您編碼的命令中

編輯: Foreach對象獲取內容

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM