我一直在嘗試遍歷 .json 組態檔串列以提取 'name'、'entity_id'、'contact_info'、'sp_endpoint'。我一直在嘗試獲取以下代碼塊,并將其轉儲到一個 csv 中,該 csv 列出了一列中的每個值,并且每一行都與該值所屬的檔案有關。
{
"sfg_ping::qa::standard_sp_connections": [
{
"name": "test",
"entity_id": "test",
"contact_info": "[email protected]",
"sp_endpoint": "test",
"sso_initialization_and_signature_policy": {
"sign_assertion_or_response": "response",
"sp_trust_model": "anchored",
"signing_certificate_id": "test",
"sp_initiated": {
"require_signed_authn_requests": true,
"primary_verification_certificate": "test"
}
}
我一直在使用此腳本來嘗試完成此操作,但它會轉儲空值,并且不處理大量檔案串列。
Get-ChildItem -Path "C:\Users\user\appdev\powershell-scripts\spConnections" |
ForEach-Object {
{(Select-String -Pattern 'name' )}
{(Select-String -Pattern 'entity_id' )}
{(Select-String -Pattern 'contact_info' )}
{(Select-String -Pattern 'sp_endpoint' )}
}| Export-Csv "C:\Users\user\appdev\powershell-scripts\export.csv"
預期的輸出將是這樣的:
| filename | name | entityid |
|:---------|:----:| --------:|
| test | test | test |
真的迷路了,只是在尋找一些方向。
uj5u.com熱心網友回復:
似乎您可以通過遵循此邏輯從您的 Json 檔案中獲取所需的資訊,但是它假定 Json 檔案只有一個父屬性,sfg_ping::qa::standard_sp_connections但是只要只有一個,這將適用于任何屬性名稱。
Get-ChildItem path\to\thejsonfolder -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 path\to\myexport.csv -NoTypeInformation
我使用 2 個不同的 Json 檔案得到的結果:
FileName name entity_id contact_info sp_endpoint
-------- ---- --------- ------------ -----------
test.json test test [email protected] test
test2.json test2 test2 [email protected] test2
如果它們都具有相同的父屬性名稱,則:
(Get-Content $_ -Raw | ConvertFrom-Json).PSObject.Properties.Value
可以替換為:
(Get-Content $_ -Raw | ConvertFrom-Json).'sfg_ping::qa::standard_sp_connections'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/512197.html
