skip to Main Content

I am trying to fetch some values from json array if condition meet

My Json File:

{
    "ImpactMitigationTime": 638159040000000000,
    "Status": "Active",
    "ExternalIncidentId": null,
    "RecommendedActions": null,
    "PlatformInitiated": true,
    "ImpactStartTime": 638114093314870000,
    "SubscriptionId": "xxxx",
    "LastUpdateTime": 638120832531660300,
    "EventSource": "ServiceHealth",
    "TrackingId": "VNY4-RC8",
    "EventLevel": "Informational",
    "impactType": null,
    "EventType": "HealthAdvisory",
    "Priority": 22,
    "duration": null,
    "Summary": "<p><em>You’re receiving this notification because you use Azure Active Directory (Azure AD).</em></p>",
    "Header": "Your service might have been impacted by an Azure service issue",
    "Impact": [
        {
            "ImpactedService": "Azure Active Directory",
            "ImpactedRegions": [
                {
                    "ImpactedRegion": "West US 2",
                    "Status": "Active"
                },
                {
                    "ImpactedRegion": "East US",
                    "Status": "Resolved"
                }
            ]
        },
        {
            "ImpactedService": "Multi-Factor Authentication",
            "ImpactedRegions": [
                {
                    "ImpactedRegion": "South Central US",
                    "Status": "Active"
                },
                {
                    "ImpactedRegion": "Central US",
                    "Status": "Resolved"
                }               
            ]
        }
    ],
    "Title": "Action required: Add your IPv6 ranges to Conditional Access policies by 31 March 2023",
    "Level": "Warning",
    "IsHIR": false
}

Need PS script to pull value only of Active status Impacted regions
My Current PS Script:

 $varNotifications = gc "C:UserspocadminDesktop1.json" | ConvertFrom-Json

if($varnotifications.Impact.ImpactedRegions | Where {$_.Status -eq 'Active'})

        {

        $varsubscriptionId = $varnotifications.subscriptionid

        $varimpactedServices = $varnotifications.Impact.ImpactedService

        
        $varimpactedRegions = $varnotifications.Impact.ImpactedRegions.ImpactedRegion

        $varstatus = $varnotifications.Impact.ImpactedRegions.status

        }
  $varsubscriptionId
  $varimpactedServices
  $varimpactedRegions
  $varstatus 

My Output:

xxxx
Azure Active Directory
Multi-Factor Authentication
West US 2
East US
South Central US
Central US
Active
Resolved
Active
Resolved 

2

Answers


  1. Your method only gets an array of the form @(), you need to get the value from it.

    $varnotifications.Impact.ImpactedRegions |Where-Object {$_.status -eq "active"} |Select-Object -Expand ImpactedRegion
    
    Login or Signup to reply.
  2. You shouldn’t try to fetch the info in various separate variables, but have the code return an array of objects with values that belong to each other.

    Something like this:

    $data = Get-Content -Path 'C:UserspocadminDesktop1.json' | ConvertFrom-Json
    # loop through the data and return only objects where the Status for the ImpactedRegion equals 'Active'
    $result = $data.impact | ForEach-Object {
        $service =  $_.ImpactedService
        foreach ($region in ($_.ImpactedRegions | Where-Object {$_.Status -eq 'Active'})) {
            [PsCustomObject]@{
                Service = $service
                Region  = $region.ImpactedRegion
                Status  = $region.Status
            }
        }
    }
    
    # output on screen
    $result
    
    # output to CSV file if you like
    $result | Export-Csv -Path 'C:UserspocadminDesktop1.csv' -NoTypeInformation -UseCulture
    

    On console screen this outputs

    Service                     Region           Status
    -------                     ------           ------
    Azure Active Directory      West US 2        Active
    Multi-Factor Authentication South Central US Active
    

    The -UseCulture switch lets PowerShell create a CSV file where the delimiter character is whatever is defined in your systems ListSeparator. By doing this, you can simply double-click the file to open it in Excel on machines that have the same regional settings


    From your comments, I gather you do not just get the objects where the ImpactedRegion’s Status equals ‘Active’, but you want to filter the results in various ways.
    To do that, just have the code output all objects and filter afterwards to whatever you find important:

    $data = Get-Content -Path 'C:UserspocadminDesktop1.json' | ConvertFrom-Json
    # loop through the data and return objects
    $result = $data.impact | ForEach-Object {
        $service =  $_.ImpactedService
        foreach ($region in $_.ImpactedRegions) {
            [PsCustomObject]@{
                Service = $service
                Region  = $region.ImpactedRegion
                Status  = $region.Status
            }
        }
    }
    

    Here you filter whatever you need from the data in $result for instance:

    # get all where the ImpactedRegion's Status equals 'Active'
    $result | Where-Object {$_.Status -eq 'Active'}
    
    # get all where the ImpactedRegion's Status is anything except 'Active'
    $result | Where-Object {$_.Status -ne 'Active'}
    
    # get all where the ImpactedRegion's Status equals 'Resolved'
    $result | Where-Object {$_.Status -eq 'Resolved'} 
    
    # get all where the ImpactedRegion's Status equals 'Active' OR 'Resolved'
    $result | Where-Object {$_.Status -match 'Active|Resolved'}  # uses regex 
    
    # get all where the ImpactedRegion's Status is anything other than 'Active' or 'Resolved'
    $result | Where-Object {$_.Status -notmatch 'Active|Resolved'}  # uses regex
    

    Etcetera, etcetera…
    Please read about Comparison operators to learn how to filter results just about anyway you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search