簡體   English   中英

如何使用PowerShell腳本讀取odata url並將其另存為json文件

[英]How to read an odata url and save it as a json file using PowerShell script

當我嘗試使用下面的PowerShell命令讀取此URL時,無法打開頁面

Start-Process "iexplore.exe" "https://sitename//odata/ScheduledJobs?$format=json&$expand=Application($select=Id,Name,AssetId;$expand=Asset($select=Id,Name)),JobType,JobInstances($filter=StartTime+eq+null;$orderby=NextRunTime+asc;$top=1),CompletedJobs($orderby=StartTime+desc;$top=1;$expand=State;$select=Id,StateId,StartTime,EndTime)&$top=20&$orderby=ScheduleStart&$filter=Enabled+eq+true&$count=true"

Get-Process | Out-File -filepath C:\Users\S.Papolu\Downloads\ScheduledJobs1.json

如何在IE中打開該網址並下載json輸出?

請對此提供幫助,感謝您的幫助,請發布代碼

您在這里有兩個問題。 我想你自己回答了第一個。 如何在Internet Explorer中打開URL? 您的start-process語句將執行此操作。

第二個問題可能與第一個問題無關:如何將JSON下載到文件中。

您可以查看invoke-webrequest cmdlet將URL的內容下載到文件中。 你想要類似的東西

$uri = "https://sitename//odata/ScheduledJobs?`$format..."
iwr -Uri $uri -OutFile C:\Users\S.Papolu\Downloads\ScheduledJobs1.json -UseBasicParsing

請注意,在URL的內容中使用雙引號,在內容中的$之前使用反引號。 您需要這樣的命令來告訴powershell將$作為文字傳遞,而不是嘗試在此處替換名為FORMAT的變量。

如果您需要憑據來訪問https://sitename那么如果您的站點使用客戶端訪問證書,則可以使用certificatecertificatethumbprint參數;如果您當前的憑據在該站點上工作,則可以使用UseDefaultCredentials參數,或者您可能需要創建一個標頭集合,並帶有Authorization標頭,並傳入有效令牌。

$headers=@{"Authorization"="Bearer Your_BearerTokenHere"}
iwr -Uri $uri -OutFile 'C:\Users\S.Papolu\Downloads\ScheduledJobs1.json' -UseBasicParsing -Headers $headers

如果uri是純REST終結點,您也可以嘗試invoke-restmethod cmdlet。

最后,您可以嘗試使用這種方法,將JSON轉換為局部變量,例如

$response = iwr ....
$json = $response.Content | ConvertFrom-JSon

除非您確實試圖將內容保存到文件中以備后用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM