2

I have been having trouble parsing an output from a rest api. it is a Multiline string

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

Now I do have an idea on how to split it by line, kinda and I try to use the following code

$data.Split("`n")

but I still cant figure out how to go about next and parse this

it doesnt recognize Policy Name, buckets, id as separate objects

do I convert it into or assign it as json or xml or something using more than just Convertto-xml or [xml] prefixing $data?

Thanks

1 Answer 1

7

Example:

$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 outputs MatchInfo objects. From these you can extract the match groups. New-Object outputs a new object based on the matches extracted from each output string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.