我正在使用 PowerShell 通過 API 從網路監視器中提取“關閉設備”資料
我正在努力解決如何參考主機名值以將其添加到變數中。我在尋找停機設備的主機名串列。
在這種情況下,只有一臺主機,但顯然,可以有多個具有不同 ID 的主機。
$user = 'user'
$pass = 'password'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
$webData = Invoke-RestMethod -Method Get -Uri 'http://fqdn/api/v0/devices/?status=0' -ContentType application/json -Headers $Headers # -OutFile c:\data\Config.html
#$webData= $webData | ConvertTo-Json
$webData | ForEach-Object {$_.devices} | Select -Property $_.hostname
來自 Powershell 的結果資料
201
---
@{device_id=201; poller_id=0; hostname=lonts01.local; sysName=lonts01.local; snmp_authname=;
不帶 -ContentType 的 $webData 的直接輸出
{"status":"ok","count":3,"devices":{"201":{"device_id":"201","poller_id":"0","hostname":"lonts01.local","sysName":"lonts01.local",...
uj5u.com熱心網友回復:
這是一個典型的例子,如果你想訪問底層物件,你需要 JSON 子鍵的名稱,因為你不能在沒有明確命名的情況下擴展它們。
請注意,我首先獲取鍵的值(名稱),然后使用它來擴展物件。
$webData.devices | Get-Member -MemberType NoteProperty | ForEach-Object {
$key = $_.Name
$webData.devices.$key.hostname
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380576.html
