簡體   English   中英

將x509證書重新分發到不同的商店

[英]Redistribute x509 certificates to different stores

我有一個p7b文件,其中包含4個證書。 但是我在幾家商店都需要它們。 因此,我首先將證書導入Cert:\\LocalMachine\\My商店,然后將其中一些Cert:\\LocalMachine\\My移到其他地方。 到目前為止,我有以下代碼:

Import-Certificate -FilePath "C:\SCOM\cert\cert_{dns name}.p7b" -CertStoreLocation Cert:\LocalMachine\My
$certIntermediate = Get-Item -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -Contains "ABB Intermediate CA"}
$certRootCA = Get-Item -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -Contains "ABB Root CA"}
$certIssuing = Get-Item -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -Contains "ABB Issuing CA"}
$store = Get-Item -Path Cert:\LocalMachine\My
$store.Open("ReadWrite")
$store.Remove($certIntermediate)
$store.Remove($certRootCA)
$store.Remove($certIssuing)
$store.Close()
$storeIntermediate = Get-Item -Path Cert:\LocalMachine\CA
$storeIntermediate.Open("ReadWrite")
$storeIntermediate.add($certIntermediate)
$storeIntermediate.close()
$storeAuthRoot = Get-Item -Path Cert:\LocalMachine\AuthRoot
$storeAuthRoot.Open("ReadWrite")
$storeAuthRoot.add($certRootCA)
$storeAuthRoot.add($certIssuing)
$storeAuthRoot.close()

忽略第一行中的{dns name}部分,這只是一般替換。 問題出在2-4行中。 如果我直接放置證書路徑(例如Cert:\\LocalMachine\\My\\8B4027E6B32E4E45C1DDB6A211 ),則腳本的其余部分將正常工作。

顯然,導入證書之前我不知道指紋,所以我不能使用它。 而且Where-Object似乎不起作用。 我試圖Get-ChildItem ,而不是Get-Item ,我想Where ,而不是Where-Object ,我想-ccontains (意外)和-like ,而不是-contains ,但證書不是“裝”的變量。 當我稍后嘗試在代碼中刪除它們時,我得到的錯誤值不能為null 如何選擇正確的證書來移動它們?

我注意到必須為您的腳本澄清三件事。

  1. 首先,問題在於cmdlet Get-Item會為您提供存儲,而不是證書:
PS> Get-Item -Path Cert:\LocalMachine\My

Name : My

您要使用的是Get-ChildItem

PS> Get-ChildItem -Path Cert:\LocalMachine\My


   PSParentPath: Microsoft.PowerShell.Security\Certificate::CurrentUser\My

Thumbprint                                Subject
----------                                -------
34BF9D3F534C2501977557CC9A48C9F5AAAAAAAA  CN=localhost

順便說一句,這解釋了為什么在提供指紋時它可以工作。 這是因為您提供了證書到Get-Item的路徑,而不是商店路徑。

PS> $cert = Get-Item -Path Cert:\CurrentUser\My\34BF9D3F534C2501977557CC9A48C9F5AAAAAAAA
PS> $cert


   PSParentPath: Microsoft.PowerShell.Security\Certificate::CurrentUser\My

Thumbprint                                Subject
----------                                -------
34BF9D3F534C2501977557CC9A48C9F5AAAAAAAA  CN=localhost
  1. 另一件事是-contains的用法。 請參閱此答案以獲取更多說明。 通常,它不是為子字符串比較而設計的。 使用其他內容(例如-like )代替:
Get-ChildItem -Path Cert:\CurrentUser\My\  | Where-Object {$_.Subject -like "*google*"}
  1. ? WhereWhere-Object別名,您可以在此處進行檢查:
PS>  get-alias | ? resolvedcommandname -eq "Where-Object"

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ? -> Where-Object
Alias           where -> Where-Object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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