我收到错误
无法检索证书,因为指纹无效。 验证指纹并重试。
当我尝试使用 LocalMachine 证书存储中的证书时。
我已经让管理员帐户在 LocalMachine 证书存储中安装了证书(包括私钥),并为某些用户提供了对私钥的访问权限(例如功能 ID)。
我希望能够运行以下代码来获取指纹,然后在
Invoke-WebRequest
调用中使用:
$certStorePath = "Cert:\LocalMachine\My"
$certDetails = Get-ChildItem -Path $certStorePath | Where-Object {$_.Subject -like "*myCert*"} # Returns one result
$certThumbprint = $certDetails.Thumbprint
Invoke-WebRequest -Uri $externalUrl -Proxy $proxyServer -UseBasicParsing -CertificateThumbprint $certThumbprint
我可以获得证书详细信息,包括指纹 ($certDetails),但似乎权限不允许我(或 FID)使用证书(或者可能只是访问证书的私钥部分)。当证书安装在 CurrentUser 存储中时,该代码可以工作。
如何为此类非管理员用户启用对 LocalMachine 存储中的证书的访问?
似乎问题与
Invoke-WebRequest
及其在这段代码中的使用方式有关。
第一部分代码能够成功访问证书:
$certStorePath = "Cert:\LocalMachine\My"
$certDetails = Get-ChildItem -Path $certStorePath | Where-Object {$_.Subject -like "*myCert*"}
但是,尽管指纹在所有证书存储中都是唯一的(例如,该指纹仅存在于 LocalMachine 中),但
Invoke-WebRequest
无法访问它,因为 it 只会在 CurrentUser 证书存储中查找。
所以,总的来说,要让这个工作正常进行:
Get-ChildItem
获取证书本身,并将 that 传递给 Invoke-WebRequest
而不是指纹:$certStorePath = "Cert:\LocalMachine\My"
$certificate = Get-ChildItem -Path $certStorePath | Where-Object {$_.Subject -like "*myCert*"} # Returns one result
Invoke-WebRequest -Uri $externalUrl -Proxy $proxyServer -UseBasicParsing -Certificate $certificate
这是 Mark-DG1 正确解决方案的一个小替代方案:
以系统身份打开 powershell。您可以使用 SysInternal 的 psexec (据我所知仍然运行良好)、任务计划程序,或者从 PSGallery
获取 Invoke-CommandAspsexec -s -i powershell
然后从随后的提示中,您可以像上面一样找到您的$证书,或者您可以直接引用指纹(对于计算机帐户个人存储中的证书):
$certificate = gi Cert:\LocalMachine\My\*<certificate thumbprint>*
Invoke-WebRequest -Uri "<targetURL>" [-Proxy $proxyServer] -UseBasicParsing -Certificate $certificate