为什么$store.Open($openFlags)
会引发异常,并且有比我的“变通方法”更好的方法来使其工作吗?
<#
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Cert:\CurrentUser\My")
$openFlags = [System.Security.Cryptography.X509Certificates.OpenFlags]::MaxAllowed
$store.Open($openFlags) #Exception calling "Open" with "1" argument(s): "The parameter is incorrect.
#>
#Work Around:
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Cert:\CurrentUser\My")
$openFlags = [System.Security.Cryptography.X509Certificates.OpenFlags]::MaxAllowed
$startIndexOfStoreName = $store.Name.LastIndexOf("\") + 1
$lengthOfStoreName = $store.Name.Length - $startIndexOfStoreName
$storeNameString = $store.Name.Substring($startIndexOfStoreName, $lengthOfStoreName)
$storeName = [System.Security.Cryptography.X509Certificates.StoreName]$storeNameString
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store($storeName, $store.Location)
$store.Open($openFlags) #No Exception thrown!
更新:似乎在使用X509Store(String)构造函数时,不允许使用任何斜杠(如果我错了,请纠正我)。因此
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My")
起作用。 最佳答案
使用以下方法定义证书存储
$store = Get-Item "Cert:\CurrentUser\My"
代替
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Cert:\CurrentUser\My")
老实说,我仍在努力弄清楚它为什么起作用,或如何起作用。
第一种方法返回一个名为“My”的$ store,因此我假设它专门针对该商店,您可以使用
$store.Open($openFlags)
第二种方法返回一个名为“Cert:\ CurrentUser \ My”的$ store。对此的打开方法将失败。