我有以下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
我也尝试过在Powershell中使用
Invoke-WebRequest
以及curl
版本进行请求,但是它不起作用Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At E:\Repos\myrepo\host\connection_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
关于powershell - Rest API调用可与curl配合使用,但不适用于PowerShell,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59932294/