Should be simple, but my inexperience is showing.
Using the similar data, I need to create INSERT to SQL Server in PowerShell 5:
{
"Libraries": {
"Reported": "2018-09-01T12:00:16",
"Locations": {
"Branch": [
{
"ID": "100",
"Address": "1 Elm Street",
"City": "Anytown",
"State": "ST",
"ZIP": "23466",
"Phone": "999-123-6543",
"Fax": "999-123-8395",
"Admin": "Doe, Jane"
},
{
"ID": "101",
"Address": "4 Main Street",
"City": "Anytown",
"State": "ST",
"ZIP": "23456",
"Phone": "999-123-4567",
"Fax": "999-123-4568",
"Admin": "Mouse, Noni"
}
]
}
}
}
First, I want to get a list as follows:
Branch Admin Address Phone Fax
------ --------- -------------------------------- ------------ -------------
100 Doe, Jane 1 Elm Street, Anytown, ST 23466 999-123-6543 999-123-8395
101 Mouse, Noni 4 Main Street, Anytown, ST 23456 999-123-4567 999-123-4568
I should do this like so, but I cannot find the way for a proper delve into the object structure:
Get-Content -Path c:\reports\libraries.json -raw | ConvertFrom-Json | ...
This will eventually feed Invoke-SQLCmd for:
Insert into Branch
(ID,Address,City,State,ZIP,Phone,Fax,Admin,Reviewed)
Values
(list from above)
The DB Reviewed column will be Reported from the JSON.
Get-Content -Raw c:\reports\libraries.json | ConvertFrom-Json | ForEach-Object { $_.Libraries.Locations.Branch } | Select -Property @{Name='Branch';Expression='ID'}, Admin, @{Name='Address';Expression={ $( $_.Address + ', ' + $_.City + ', ' + $_.State + ' ' + $_.ZIP ) } }, Phone, Fax | Format-Table