I am trying to parse the output of a rest api query of the form
$response = Invoke-RestMethod -Uri $uri -Headers $headers
$response.name | Select-String -Pattern ^role
returns an output similar to this below (elements separated by ::)
role::servicing2
role::collaboration::lei
role::commercial_lines::npds
role::nvp::windows::ucce_gold
role::oracle::linux::oracle_oid
role::splunk::splunk_enterprise::add_on
I need to read this output line by line and parse.
If there are just 2 elements eg. role::servicing2 ignore the line
If there are 3 elements, ignore the first element "role", prepend puppet_ to the second element and it becomes the project, the third element is the role (OS is unknown)
If there are 4 or more elements, ignore the first element "role", prepend puppet_ to the second element and it becomes the project, if the third element is "windows" or "linux" that is the OS, else OS is "unknown", and the last element \:\:'(\w+)'$ is the role.
Need an output in the form of an array or table or list in this format (Don't necessarily need header)
Project OS Role
puppet_collaboration unknown lei
puppet_commercial_lines unknown npds
puppet_nvp windows ucce_gold
puppet_oracle linux oracle_oid
puppet_splunk unknown add_on
I have tried various regex expressions. Couldn't figure out the logic of walking this line by line and parsing appropriately into a list or array.