首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将x509证书重新分发到不同的存储区

将x509证书重新分发到不同的存储区
EN

Stack Overflow用户
提问于 2019-07-02 12:03:52
回答 1查看 307关注 0票数 1

我有一个p7b文件,其中包含4个证书。但我需要他们在几家商店里。因此,我首先在Cert:\LocalMachine\My存储中导入证书,然后需要将其中一些证书移到其他地方。到目前为止,我有这样的代码:

代码语言:javascript
复制
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\My\8B4027E6B32E4E45C1DDB6A211),那么脚本的其余部分就能工作了。

显然,在导入证书之前,我不知道指纹,所以我不能使用。而且Where-Object似乎不起作用。我尝试了Get-ChildItem而不是Get-Item,尝试了Where而不是Where-Object,尝试了-ccontains (意外)和-like而不是-contains,但是证书并没有“加载”到变量中。当我试图删除他们在后面的代码,我得到的错误值不能为空。如何选择正确的证书来移动它们?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-02 20:39:21

我注意到有三件事必须为你的剧本澄清。

  1. 首先,问题是cmdlet Get-Item为您提供存储,而不是证书:
代码语言:javascript
复制
PS> Get-Item -Path Cert:\LocalMachine\My

Name : My

你想要使用的是Get-ChildItem

代码语言:javascript
复制
PS> Get-ChildItem -Path Cert:\LocalMachine\My


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

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

顺便说一句,这解释了为什么当你提供拇指指纹时它能工作。这是因为您为Get-Item提供了cert的路径,而不是存储路径。

代码语言:javascript
复制
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的用法。有关更多解释,请参见this answer。通常,它不是为子字符串比较而设计的。使用其他东西(例如-like)来代替:
代码语言:javascript
复制
Get-ChildItem -Path Cert:\CurrentUser\My\  | Where-Object {$_.Subject -like "*google*"}
  1. ?WhereWhere-Object的别名,您可以在这里查看:
代码语言:javascript
复制
PS>  get-alias | ? resolvedcommandname -eq "Where-Object"

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ? -> Where-Object
Alias           where -> Where-Object
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56852056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档