我正在尝试调用 Azure Rest API 并获取 DevTestLabs 的时间表。我尝试了 Invoke-RestMethod,但它没有给出“dailyRecurrence”键的值。但 Invoke-WebRequest 可以。
原因是什么?
网址
$url = "https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourseGroup}/providers/Microsoft.DevTestLab/labs/{LabName}/schedules/LabVmsShutdown?api-version=2018-10-15-preview"
Run Code Online (Sandbox Code Playgroud)
带有 $expand 的 URL
$url = "https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourseGroup}/providers/Microsoft.DevTestLab/labs/{LabName}/schedules/LabVmsShutdown?$expand=properties(dailyRecurrence)&api-version=2018-10-15-preview"
Run Code Online (Sandbox Code Playgroud)
调用 Invoke-RestMethod
$output = Invoke-RestMethod -Uri $url -Method "GET" -ContentType "application/json" -Headers $authHeaders
properties : @{status=Enabled; taskType=LabVmsShutdownTask; dailyRecurrence=; timeZoneId=AUS Eastern Standard Time;
notificationSettings=; createdDate=26/03/2019 4:38:18 PM; provisioningState=Succeeded;
uniqueIdentifier=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}
Run Code Online (Sandbox Code Playgroud)
调用Invoke-WebRequest
$output = Invoke-WebRequest -Uri $url -Method "GET" -Headers $authHeaders
Content : {"properties":{"status":"Enabled","taskType":"LabVmsShutdownTask","dailyRecurrence":{"time":"1900"}
,"timeZoneId":"AUS Eastern Standard Time","notificationSettings":{"status":"Disabled","timeInMinute
s":30},"createdDate":"2019-03-26T03:38:18.0726376+00:00","provisioningState":"Succeeded","uniqueIde
ntifier":"XXXXXXXXXXXXXXXXXXXXXXXXX"},"id":"/subscriptions/XXXXXXXXXXXXXXXXXXX/resourcegroups/XXXXXXXXXXXXX/providers/microsoft.devtestlab/labs/XXXXXXXX/schedules/labvmsshutdown","name":"LabVmsShutdown","type":"microsoft.devtestlab/labs/schedules","location":"australiasoutheast"}
Run Code Online (Sandbox Code Playgroud) powershell invoke-webrequest azure-rest-api invoke-restmethod
我们Invoke-RestMethod在 PowerShell 脚本中使用来调用GET具有可变长度运行时的方法端点。有些电话可能会在几秒钟后返回,有些可能需要长达 20 分钟。我们通过-TimeoutSec参数设置了 50 分钟的通话超时。
只需几秒钟的调用即可正常返回并输出预期的响应。更长的调用(例如 5 分钟)永远不会返回并且Invoke-RestMethod命令用完整个 50 分钟超时,尽管我们在 Web 服务器日志中确认服务器早已返回200 OK.
try
{
$Url = "https://example.com/task" # GET
$Timeout = 3000 # 50 minute timout
$Response = Invoke-RestMethod $Url -TimeoutSec $Timeout
Write-Host $Response
}
catch
{
Write-Host $_.Exception
}
Run Code Online (Sandbox Code Playgroud)
端点上没有身份验证。PowerShell 版本为 7。该脚本在托管被调用的 Web 服务器的同一台机器上运行。
这是Invoke-RestMethod我们不知道的配置问题吗?我们在Invoke-WebRequest使用基本相同的脚本时遇到了类似的问题。
我需要对网络服务器进行 POST 调用,该服务器正在验证 cookie 中的用户类型,我不知道如何将此 cookie 添加到我的请求中。
$eth_config = Invoke-RestMethod -Method 'Post' -Uri $network_settings_url -Body $request_body
Run Code Online (Sandbox Code Playgroud) 参考`Invoke-RestMethod的帮助文件:
PS /home/nicholas>
PS /home/nicholas> $response = Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/
PS /home/nicholas>
PS /home/nicholas> $json = $response | ConvertTo-Json
WARNING: Resulting JSON is truncated as serialization has exceeded the set depth of 2.
PS /home/nicholas>
PS /home/nicholas> $xml = $response | ConvertTo-Xml
PS /home/nicholas>
PS /home/nicholas> $xml.OuterXml
Run Code Online (Sandbox Code Playgroud)
如何将其转换response为xml并在一行中输出(如上所述)?
根据https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7#example-2--run-a-post-request我是试图调用一个简单的 POST 方法,但遇到一些错误。
我的指示是:
$uri = "https://localhost:44355/api/job/machine-status";
#$machineName = HOSTNAME.EXE;
$machineName = "simPass2";
$body = @{
Name = $machineName
Status = "Complete"
}
Invoke-RestMethod -Method 'Post' -Uri $uri -ContentType 'application/json' -Body $body;
Run Code Online (Sandbox Code Playgroud)
我的错误是
Invoke-WebRequest : Unable to connect to the remote server
At line:8 char:1
+ Invoke-WebRequest -Uri $uri -Method Post -ContentType 'application/js ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Comman
ds.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)