We are trying to Pull AP names and two specific Bssid values out of json files provided by network team, in PowerShell 5.1
Started with the following function to get the names (this works for just the names), but am stumped on how to get ChildItems, or if it’s even possible to pull child items when searching a string like this, unlikely would be my guess.
Function GetName {
$WapName = get-content $FileName -Raw |
Select-String -pattern '"name":s*"?([^",]+)"?,?' -allmatches |
Foreach-Object { $_.Matches | ##Get-childItem matching specific ESSID, return macaddr ??
Foreach-Object { $_.Groups[1].Value}
}
if ($null -eq $WapName){
Write-Host "Array is null"
}# End of If
else{
Write-Host "Array has $($WapName.length) elements"
$WapName
}#End of Else
}#End of Function
Thinking we may need to convertFrom-Json to achieve child item pulls, but am unfamiliar with how to work with that data. want to Ultimately get this converted to a csv file for manipulation.
Here’s example of JSON file. We need AP Names and each associated wifi macaddr value for the wifitrusted and wifiguest items only. The rest of the data is unneeded.
{
"aps": [
{
"macaddr": "xx:xx:xx:xx:xx:xx",
"name": "**WAP01**",
"radio_bssids": [
{
"bssids": [
{
"essid": "wifiguest",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "wifitrusted",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "Wifi-xyz",
"macaddr": "xx:xx:xx:xx:xx:xx"
}
],
"index": 0,
"macaddr": "xx:xx:xx:xx:xx:xx"
},
{
"bssids": [
{
"essid": "wifiguest",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "wifitrusted",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "Wifi-xyz",
"macaddr": "xx:xx:xx:xx:xx:xx"
}
],
"index": 1,
"macaddr": "xx:xx:xx:xx:xx:xx"
}
],
"serial": "sjfdgkjafdgl",
"swarm_id": "kjslkfdjgaofdsgj0w94rlkjo9wrjliwedlkfjwo3"
},
{
"macaddr": "xx:xx:xx:xx:xx:xx",
"name": "**WAP02**",
"radio_bssids": [
{
"bssids": [
{
"essid": "wifiguest",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "wifitrusted",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "Wifi-xyz",
"macaddr": "xx:xx:xx:xx:xx:xx"
}
],
"index": 0,
"macaddr": "xx:xx:xx:xx:xx:xx"
},
{
"bssids": [
{
"essid": "wifiguest",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "wifitrusted",
"macaddr": "**xx:xx:xx:xx:xx:xx**"
},
{
"essid": "Wifi-xyz",
"macaddr": "xx:xx:xx:xx:xx:xx"
}
],
"index": 1,
"macaddr": "xx:xx:xx:xx:xx:xx"
}
],
"serial": "lasjdhfad",
"swarm_id": "lkasdjlkajsd;kasdfja;lsdkjffkjlkfj"
},
],
"count": 2
}
Preferred Output would be something like this repeated for each WAP with ALL records containing wifitrusted and wifiguest:
WAP ESSID MACAddr
WAP01 wifitrusted xx:xx:xx:xx:xx:xx
WAP02 wifiguest xx:xx:xx:xx:xx:xx
Thanks for the assist.
Tried going this route in an attempt to get wifiguest to return, this is broken and just a placeholder at this point really as it is not fully what is needed. We are importing the json file into parameter $FileName.
Function GetEssid {
$ESSID = [ordered]@{}
$ESSID = (get-content $FileName -Raw | Convertfrom-Json).PSobject.Properties |
ForEach-Object {$ESSID[$_.essid] = $_.Value }
$ESSID.Stuffs.Where({$_.essid -eq "wifiguest"}).Value
2
Answers
Presumably something like this should help, it’s unclear if you just want 2 records as-in your sample or just all records where
ESSID
iswifiguest
orwifitrusted
but this should be a start.Using the sample Json provided in question the output from the above is:
Continuing on the helpful answer from Santiago Squarzon with a general approach:
Iterating through an object-graph resulted from cmdlets as
ConvertFrom-Json
appears to be a quiet common use-case in PowerShell. Therefore I have tagged several other examples with: powershell/object-graphIn your case, it might come in handy to understand the path a of specific (leaf) node, e.g.:
This might help you to address the specific nodes.
Using the included Extended Dot Notation expression you might even easier target specific nodes and avoid several embedded
foreach
loops.In the example below I am also using the standard PowerShell feature called calculated properties:
Explanation:
~
search for any leaf node named:essid
and a value of:=wifiguest
or:/wifitrusted
aps.radio_bssids.bssids.essid=wifiguest/wifitrusted
could also be used here)......Name
mean: from the current node, go5
parents up (a single dot –.
– represents the current node) and select the child node namedName
.