我正在嘗試呼叫 Azure Rest API 并獲取 DevTestLabs 的計劃。我試圖呼叫-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"
帶有 $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"
呼叫 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}
呼叫 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"}
uj5u.com熱心網友回復:
問題:
只是顯示問題,
盡管暴露了一個長期存在的錯誤,但在 PowerShell Core 7.2.0-rc.1 中仍然存在,即
[pscustomobject]實體錯誤地字串化為空字串- 請參閱GitHub 問題 #6163。
簡而言之:
- 該資料是存在的
Invoke-RestMethod輸出,它只是似乎是失蹤-請參閱下一節。 - 使用
$output.properties.dailyRecurrence看看吧,或者更有益可視化的輸出,用它$output |ConvertTo-Json來重新將它轉換為(美化)JSON。- 注意:在某些情況下,您可能需要添加一個
-Depth引數來完全表示深度大于 2 的物件圖 - 請參閱此帖子。
- 注意:在某些情況下,您可能需要添加一個
Invoke-RestMethod- 不同于Invoke-WebRequest-內置反序列化:使用 JSON 回應,它會自動將回傳的 JSON 文本決議為[pscustomobject]圖形,就像ConvertFrom-Json已應用于它一樣。
您看到的是生成的物件圖的默認格式,這并不真正適合可視化嵌套 [pscustomobject]實體,這些實體由它們的.ToString()值表示- 因此 - 由于錯誤 - 可能看起來沒有價值,即使它們確實如此.
相比之下,由于輸出Invoke-WebRequest在.Content其輸出物件的屬性中按原樣報告 JSON 文本,因此問題不會出現在那里。
該錯誤的簡單演示:
[pscustomobject] @{
nested =
[pscustomobject] @{
deep =
[pscustomobject] @{
deepest = 'down'
}
}
}
從 PowerShell Core 7.2.0-rc.1 開始,這會產生以下結果,錯誤地暗示它.nested.deep沒有價值,即使它顯然確實具有價值:
nested
------
@{deep=}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/338505.html
上一篇:如何擴展變數中的屬性
