使用cer/pem进行身份验证时,与给定curl等效的powershell是什么?

使用cer/pem进行身份验证时,与给定curl等效的powershell是什么?,powershell,authentication,curl,Powershell,Authentication,Curl,我有一个针对rest服务的curl命令,该服务需要身份验证,如下所示: curl -s --key private_key.pem --cert certificate.cer $url 现在,这个命令可以完美地返回有效的json 由于一些项目要求,现在我需要使用powershell。为此,我尝试使用invokerestmethod,如下所示 Invoke-RestMethod -Uri $url -Method Get -Certificate certificate.cer $cert

我有一个针对rest服务的curl命令,该服务需要身份验证,如下所示:

curl -s --key private_key.pem --cert certificate.cer $url
现在,这个命令可以完美地返回有效的json

由于一些项目要求,现在我需要使用powershell。为此,我尝试使用invokerestmethod,如下所示

Invoke-RestMethod -Uri $url -Method Get -Certificate certificate.cer
$cert = Get-PfxCertificate -FilePath .\certificate.pfx
$response = Invoke-RestMethod -Uri $url -Method Get -Certificate $cert
我给出的错误如下所示:

Invoke-RestMethod : Cannot bind parameter 'Certificate'. Cannot convert value ".\certificate.cer" to type 
"System.Security.Cryptography.X509Certificates.X509Certificate". Error: "The system cannot find the file specified.
我还尝试将证书添加到windows中的证书存储中。现在查询$url会返回未经授权的响应


请帮助我使用pem和cer文件对服务进行身份验证。没有为任何替代方法提供用户名/密码。

不过,我通过使用openssl将证书转换为pfx证书来实现这一点

openssl pkcs12 -export -out .\certificate.pfx -inkey .\private_key.pem -in .\certificate.cer
然后我使用Invoke rest方法,如下所示

Invoke-RestMethod -Uri $url -Method Get -Certificate certificate.cer
$cert = Get-PfxCertificate -FilePath .\certificate.pfx
$response = Invoke-RestMethod -Uri $url -Method Get -Certificate $cert

我得到了一个有效的JSON响应。谢谢您的帮助。

$cert=[System.Security.Cryptography.X509Certificates.X509Certificate]::CreateFromCertFile('C:\path\to\certificate.cer');调用RestMethod-Uri$url-Method Get-Certificate$cert@MathiasR.Jessen,谢谢你的代码片段,但是当我尝试时,我仍然会得到未经授权的响应。我是否也需要以某种方式使用pem文件?