基本上,我编写了一个脚本来解析json对象,并将json文件中的某些信息弹出到excel文档中。这些文件最初是.yaml文件,我使用工具对它们进行了转换。我发现有些格式不正确,不允许所有的格式都转换为json格式。因此,我安装了powershell-yaml模块,并尝试使用ConvertFrom-YAML,并且脚本没有提取与我的json脚本相同的信息。如果有一种使用json提取数据的方法,但将文件作为.yaml保存在它们的原始目录中,那么您可以自由地共享!
工作JSON代码:
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代码:
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
样本.yaml输出:
---
sfg_hhh::production::standard_sp_connections:
- name: RRRRR
entity_id: WWWWWW
contact_info: ggggg@ggggg.com
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:
预期产出:
目前的产出:
发布于 2022-11-03 20:00:41
yaml中第一个属性的名称不太好处理:/
所以你必须绕开它,因为你已经试过了。
我有一个小技巧,可以通过JSON传递从ConvertFrom接收到的对象。
$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
编辑:更改为获取当前文件夹下的所有YAML文件。
https://stackoverflow.com/questions/74307257
复制相似问题