将 x509 证书重新分发到不同的商店
Redistribute x509 certificates to different stores
我有一个p7b文件,里面有4个证书。但我需要在几家商店使用它们。所以我首先在 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 名称} 部分,这只是一般替换。问题在第 2-4 行。如果我直接放置证书路径(如 Cert:\LocalMachine\MyB4027E6B32E4E45C1DDB6A211
),则脚本的其余部分有效。
显然在导入证书之前我不知道指纹,所以我不能使用它。 Where-Object
似乎不起作用。我试过 Get-ChildItem
而不是 Get-Item
,我试过 Where
而不是 Where-Object
,我试过 -ccontains
(不小心)和 -like
而不是 -contains
,但证书不是 "loaded" 变量。当我稍后尝试在代码中删除它们时,出现错误 value cannot be null。我怎样才能 select 移动它们的正确证书?
我注意到您的脚本需要澄清三件事。
- 首先,问题是 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\MyBF9D3F534C2501977557CC9A48C9F5AAAAAAAA
PS> $cert
PSParentPath: Microsoft.PowerShell.Security\Certificate::CurrentUser\My
Thumbprint Subject
---------- -------
34BF9D3F534C2501977557CC9A48C9F5AAAAAAAA CN=localhost
- 另一件事是
-contains
的用法。请参阅 this answer 了解更多说明。通常,它不是为子字符串比较而设计的。使用其他东西(例如 -like
)代替:
Get-ChildItem -Path Cert:\CurrentUser\My\ | Where-Object {$_.Subject -like "*google*"}
?
和Where
是Where-Object
的别名,你可以在这里查看:
PS> get-alias | ? resolvedcommandname -eq "Where-Object"
CommandType Name Version Source
----------- ---- ------- ------
Alias ? -> Where-Object
Alias where -> Where-Object
我有一个p7b文件,里面有4个证书。但我需要在几家商店使用它们。所以我首先在 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 名称} 部分,这只是一般替换。问题在第 2-4 行。如果我直接放置证书路径(如 Cert:\LocalMachine\MyB4027E6B32E4E45C1DDB6A211
),则脚本的其余部分有效。
显然在导入证书之前我不知道指纹,所以我不能使用它。 Where-Object
似乎不起作用。我试过 Get-ChildItem
而不是 Get-Item
,我试过 Where
而不是 Where-Object
,我试过 -ccontains
(不小心)和 -like
而不是 -contains
,但证书不是 "loaded" 变量。当我稍后尝试在代码中删除它们时,出现错误 value cannot be null。我怎样才能 select 移动它们的正确证书?
我注意到您的脚本需要澄清三件事。
- 首先,问题是 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\MyBF9D3F534C2501977557CC9A48C9F5AAAAAAAA
PS> $cert
PSParentPath: Microsoft.PowerShell.Security\Certificate::CurrentUser\My
Thumbprint Subject
---------- -------
34BF9D3F534C2501977557CC9A48C9F5AAAAAAAA CN=localhost
- 另一件事是
-contains
的用法。请参阅 this answer 了解更多说明。通常,它不是为子字符串比较而设计的。使用其他东西(例如-like
)代替:
Get-ChildItem -Path Cert:\CurrentUser\My\ | Where-Object {$_.Subject -like "*google*"}
?
和Where
是Where-Object
的别名,你可以在这里查看:
PS> get-alias | ? resolvedcommandname -eq "Where-Object"
CommandType Name Version Source
----------- ---- ------- ------
Alias ? -> Where-Object
Alias where -> Where-Object