Rest API 调用适用于 curl,但不适用于 PowerShell



我有以下curl有效的请求

curl.exe -X GET "https://host.crm.dynamics.com/api/data/v9.1/accounts?$select=name" -H "Authorization: Bearer xxxxx"

并希望在PowerShell中实现它

Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
-Headers @{Authorization = "Bearer xxxxx"} `
-Method  Get

我还尝试使用Invoke-WebRequest以及Powershell中的curl版本进行请求,但它不起作用

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At E:Reposmyrepohostconnection_test.ps1:51 char:1
+ Invoke-RestMethod $hostName `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

使用 Invoke-RestMethod 建立连接时,您需要确保客户端使用的加密被 URI 接受。

默认情况下,PowerShell 对 Web 请求使用 TLS 1.0,但要连接到的 URI 可能使用 1.1 或 1.2。您可以使用以下命令强制使用 TLS v1.1 或 v1.2。

[Net.ServicePointManager]::SecurityProtocol = `
[Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12;
Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
-Headers @{Authorization = "Bearer xxxxx"} `
-Method  Get

最新更新