I have a script to get a list of ip address in a section of a json file. If the name is equal to AzureCloud.westeurope. The name and the ips are at different levels of the json file:
In my script I get my Json file and ConvertFrom-Json. I search the list of names to match AzureCloud.westeurope and then want to return the list of ips that are associated with that name. I then try to return the ips if the object name is equal to AzureCloud.westeurope. But the $ips variables is empty after I run my script. What am I dong wrong?
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
CodePudding user response:
something like this should work:
function GetIpListForRegion {
$data = (Get-Content $output | ConvertFrom-Json)
$data.values | Where-Object { $_.Name -EQ 'AzureCloud.westeurope' } | ForEach-Object { $_.properties.addressPrefixes }
}
or even shorter:
function GetIpListForRegion {
(Get-Content $output | ConvertFrom-Json).values.Where{ $_.Name -EQ 'AzureCloud.westeurope' }.ForEach{ $_.properties.addressPrefixes }
}
To get prefixes for all regions into a single array:
(Get-Content 'C:\Temp\ServiceTags_Public_20220214.json' | ConvertFrom-Json).values.ForEach{
[pscustomobject]@{
Name = $_.Name
Prefixes = ($_.properties.addressPrefixes -join ' ,')
}
}