[英]Getting ip addresses from json file using powershell
我有一個腳本可以在 json 文件的一部分中獲取 ip 地址的列表。 如果名稱等於 AzureCloud.westeurope。 name和ips在json文件的不同層級:
在我的腳本中,我得到了我的 Json 文件和 ConvertFrom-Json。 我搜索名稱列表以匹配 AzureCloud.westeurope,然后想要返回與該名稱關聯的 ips 列表。 然后,如果 object 名稱等於 AzureCloud.westeurope,我會嘗試返回 ips。 但是在我運行我的腳本后 $ips 變量是空的。 我哪里錯了?
function GetIpListForRegion {
$data = (Get-Content $output | ConvertFrom-Json)
foreach($property in $data.values.name) {
if($property -eq "AzureCloud.westeurope") {
$ips = $data.values.properties.addressPrefixes | Where-Object name -eq "AzureCloud.westeurope"
Write-Host $ips
這樣的事情應該有效:
function GetIpListForRegion {
$data = (Get-Content $output | ConvertFrom-Json)
$data.values | Where-Object { $_.Name -EQ 'AzureCloud.westeurope' } | ForEach-Object { $_.properties.addressPrefixes }
}
甚至更短:
function GetIpListForRegion {
(Get-Content $output | ConvertFrom-Json).values.Where{ $_.Name -EQ 'AzureCloud.westeurope' }.ForEach{ $_.properties.addressPrefixes }
}
將所有區域的前綴放入一個數組中:
(Get-Content 'C:\Temp\ServiceTags_Public_20220214.json' | ConvertFrom-Json).values.ForEach{
[pscustomobject]@{
Name = $_.Name
Prefixes = ($_.properties.addressPrefixes -join ' ,')
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.