繁体   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