我有一个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\My\8B4027E6B32E4E45C1DDB6A211),那么脚本的其余部分就能工作了。
显然,在导入证书之前,我不知道指纹,所以我不能使用。而且Where-Object似乎不起作用。我尝试了Get-ChildItem而不是Get-Item,尝试了Where而不是Where-Object,尝试了-ccontains (意外)和-like而不是-contains,但是证书并没有“加载”到变量中。当我试图删除他们在后面的代码,我得到的错误值不能为空。如何选择正确的证书来移动它们?
发布于 2019-07-02 20:39:21
我注意到有三件事必须为你的剧本澄清。
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提供了cert的路径,而不是存储路径。
PS> $cert = Get-Item -Path Cert:\CurrentUser\My\34BF9D3F534C2501977557CC9A48C9F5AAAAAAAA
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-Objecthttps://stackoverflow.com/questions/56852056
复制相似问题