我在解析rest api的输出时遇到了问题。 它是一个多行字符串
Policy Name: Default_US_MultiSite Id: abc1234abc123 Buckets: support_us1_multisite,ch1ny2
Policy Name: Default_CH Id: 123456789acdef Buckets: question,answer,ch2,ch1,drive.me.closer
Policy Name: Default_NY Id: qrstuvwxyz9876 Buckets: demo,bucket1,test1,test,ny0,nyhello,goodbye,new.shoes,pizza,cutecats,theinternetisfor,Halloween,For-the-emperor
现在我确实知道如何按行分割,有点我尝试使用以下代码
$data.Split("`n")
但我仍然无法弄清楚如何进行下一步并解析此
它不会将Policy Name,buckets,id识别为单独的对象
我是否将其转换为json或xml或者使用不仅仅是Convertto-xml或[xml]前缀$ data的东西?
谢谢
答案 0 :(得分:2)
示例:
$data = @"
Policy Name: Default_US_MultiSite Id: abc1234abc123 Buckets: support_us1_multisite,ch1ny2
Policy Name: Default_CH Id: 123456789acdef Buckets: question,answer,ch2,ch1,drive.me.closer
Policy Name: Default_NY Id: qrstuvwxyz9876 Buckets: demo,bucket1,test1,test,ny0,nyhello,goodbye,new.shoes,pizza,cutecats,theinternetisfor,Halloween,For-the-emperor
"@
$data -split "`n" |
Select-String 'Policy Name: (\w+) Id: (\w+) Buckets: (.+)' |
ForEach-Object {
New-Object PSObject -Property ([Ordered] @{
"PolicyName" = $_.Matches[0].Groups[1].Value
"Id" = $_.Matches[0].Groups[2].Value
"Buckets" = $_.Matches[0].Groups[3].Value
})
}
Select-String
输出MatchInfo
个对象。从这些中,您可以提取匹配组。 New-Object
根据从每个输出字符串中提取的匹配项输出新对象。