将Invoke-RestMethod输出解析为csv文件

时间:2019-06-11 19:16:55

标签: powershell parsing

我需要解析Invoke-RestMethod的输出,以将元素提取到csv文件中的单独列中,以便可以导入数据库。任何帮助将不胜感激。

尝试了foreach以及split和正则表达式

$trusted_facts = @{
query=’["from", "facts",
    ["extract", ["certname", "environment", "value"],
      ["=", "name", "trusted"]
    ]
  ]’
}

$jsonbody = $trusted_facts | ConvertTo-Json

$response = Invoke-RestMethod -Method POST -Uri $uri -Headers $headers - 
Body $jsonbody -ContentType 'application/JSON'

$response.value | Format-List

$response.value | Export-Csv -Path $csvFileName -NoTypeInformation

这是我将其通过管道传输到Format-List时的输出

domain        : prci.com
certname      : xobqpupm.prci.com
hostname      : xobqpupm
extensions    : 
authenticated : remote

domain        : proghszq.com
certname      : scpupt03.proghszq.com
hostname      : scpupt03
extensions    : 
authenticated : remote

domain        : proghszq.com
certname      : scpupq13.proghszq.com
hostname      : scpupq13
extensions    : @{pp_role=test_server; pp_apptier=development; pp_project=corporate; pp_department=puppet_common}
authenticated : remote

domain        : proghszq.com
certname      : scchocot01.proghszq.com
hostname      : scchocot01
extensions    : @{pp_role=chocotest; pp_apptier=production; pp_project=puppet_ets; pp_department=compute}
authenticated : remote

这是我通过管道导出到Export-CSv时的输出

"prci.com","xobqpupm.prci.com","xobqpupm","","remote"
"proghszq.com","scpupt03.proghszq.com","scpupt03","","remote"
"proghszq.com","scpupq13.proghszq.com","scpupq13","@{pp_role=test_server; pp_apptier=development; pp_project=corporate; pp_department=puppet_common}","remote"
"proghszq.com","scchocot01.proghszq.com","scchocot01","@{pp_role=chocotest; pp_apptier=production; pp_project=puppet_ets; pp_department=compute}","remote"

这是我想要的输出(即,在扩展中填充元素时,提取元素并放在单独的列中)

"proghszq.com","scpupq13.proghszq.com","scpupq13","test_server","development","corporate","puppet_common"
"proghszq.com","scchocot01.proghszq.com","scchocot01","chocotest","production","puppet_ets","compute"

2 个答案:

答案 0 :(得分:1)

Export-Csv:假设所有输入中的计数相同,则转换为CSV文件。使用calculated properties

$response.value | Select-Object -Property *,
    @{ n='pp_role';
       e={ if ('pp_role' -in $_.extensions.psobject.Properties.name) 
                {$_.extensions.pp_role} else {''} }},
    @{ n='pp_apptier';
       e={ if ('pp_apptier' -in $_.extensions.psobject.Properties.name) 
                {$_.extensions.pp_apptier} else {''} }},
    @{ n='pp_project';
       e={ if ('pp_project' -in $_.extensions.psobject.Properties.name) 
                {$_.extensions.pp_project} else {''} }},
    @{ n='pp_department';
       e={ if ('pp_department' -in $_.extensions.psobject.Properties.name) 
                {$_.extensions.pp_department} else {''} }}

针对$aux而不是$response.value进行了测试,其中$aux的定义如下:

$aux = @(
    [PSCustomObject]@{
        domain        = 'A.proghszq.com'
        certname      = 'A.scpupq13.proghszq.com'
        hostname      = 'A.scpupq13'
        extensions    = ''
        authenticated = 'A.remote'
    },
    [PSCustomObject]@{
        domain        = 'proghszq.com'
        certname      = 'scpupq13.proghszq.com'
        hostname      = 'scpupq13'
        extensions    = [PSCustomObject]@{ 
                            pp_role      ='test_server'
                            pp_apptier   ='development'
                            pp_project   ='corporate'
                            pp_department='puppet_common'
                        }
        authenticated = 'remote'
    }
)

输出

domain        : A.proghszq.com
certname      : A.scpupq13.proghszq.com
hostname      : A.scpupq13
extensions    : 
authenticated : A.remote
pp_role       : 
pp_apptier    : 
pp_project    : 
pp_department : 

domain        : proghszq.com
certname      : scpupq13.proghszq.com
hostname      : scpupq13
extensions    : @{pp_role=test_server; pp_apptier=development; pp_project=corporate; pp_department=puppet_common}
authenticated : remote
pp_role       : test_server
pp_apptier    : development
pp_project    : corporate
pp_department : puppet_common

答案 1 :(得分:0)

这是另一种过滤掉扩展字段中没有条目的响应的方法:

$aux | where {($_.extensions -ne $null) -and ($_.extensions -ne '')} |
    select domain, certname, hostname, 
    @{n='pp_role';e={$_.extensions.pp_role}},
    @{n='pp_apptier';e={$_.extensions.pp_apptier}}, 
    @{n='pp_project';e={$_.extensions.pp_project}},
    @{n='pp_department';e={$_.extensions.pp_department}} |
      ConvertTo-Csv -NoTypeInformation