I basically wrote a script to parse json objects and pop certain information out of the json file into an excel doc. Well those files were originally .yaml, and I converted them using a tool. I found that some have incorrect formatting which didnt allow all of them to convert to json format. So I installed the powershell-yaml module and attempted to use ConvertFrom-YAML and the script is not pulling the same information as my json script. If theres a way to pull that data using json, but keep the files as a .yaml in their original directory, feel free to share!
Working JSON Code:
Get-ChildItem "C:\Users\hhh\appdev\powershell-scripts\spConnections" -Filter *.json -PipelineVariable file | ForEach-Object {
(Get-Content $_ -Raw | ConvertFrom-Json).PSObject.Properties.Value | Select-Object @(
@{ N = 'FileName'; E = { $file.Name }}, 'Name', 'entity_id', 'contact_info', 'sp_endpoint'
)
} | Export-Csv "C:\Users\hhh\appdev\powershell-scripts\dataexports\test.csv" -NoTypeInformation
YAML Code that doesnt work:
Get-ChildItem "C:\Users\hhh\appdev\targetfolder" -Recurse -Filter *.yaml -PipelineVariable file | ForEach-Object {
(Get-Content $_ -Raw | ConvertFrom-Yaml).PSObject.Properties.Value | Select-Object @(
@{ N = 'FileName'; E = { $file.Name }}, 'Name', 'entity_id', 'contact_info', 'sp_endpoint'
)
} | Export-Csv "C:\Users\hhh\appdev\powershell-scripts\dataexports\test.csv" -NoTypeInformation
Sample .yaml output:
---
sfg_hhh::production::standard_sp_connections:
- name: RRRRR
entity_id: WWWWWW
contact_info: [email protected]
sp_endpoint: CCCCC
base_index: 0
sso_initialization_and_signature_policy:
signing_certificate_id: EEEEE
sp_initiated:
require_signed_authn_requests: false
authentication_policy_contracts:
- LDAPUser
attribute_contract:
CodePudding user response:
The name of the first property in the yaml isn't very nice to handle :/
So you have to bypass it as you've already tried too.
I've got a little trick to pivot the object received from ConvertFrom-Yaml, by passing it through JSON.
$files = get-childitem "C:\Users\hhh\appdev\powershell-scripts\spConnections" `
-Filter "*.yaml" -Recurse -File
[array]$array = @()
foreach ($file in $files){
$object = (
get-content $file | ConvertFrom-Yaml |
ConvertTo-Json | ConvertFrom-Json
).PSobject.Properties.Value | select name,contact_info
$object = $object | Select @{l='filename'; e={$file.Name}}, *
$array = $object
}
# $array | ConvertTo-CSV -NoTypeInformation
$array | Export-Csv -Path ./path.csv -NoTypeInformation
Edit: Changed to fetching all YAML-files below the current folder.