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
Your method only gets an array of the form @(), you need to get the value from it.
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:
On console screen this outputs
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 settingsFrom 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:
Here you filter whatever you need from the data in $result for instance:
Etcetera, etcetera…
Please read about Comparison operators to learn how to filter results just about anyway you want.