Created
June 21, 2023 13:07
-
-
Save JJK96/d8a7998606d23c1b911cb1c62401b509 to your computer and use it in GitHub Desktop.
Powershell Convert list of : separated fields to actual objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Input example | |
Name: test | |
A: abc | |
B: def | |
Name: obj2 | |
A: xyz | |
B: abc | |
*/ | |
function to-objects { | |
BEGIN { | |
$object = New-Object -TypeName PSCustomObject | |
} | |
process { | |
if($_ -ne ""){ | |
$key, $value = $_ -split ":" | |
$object | Add-Member -MemberType NoteProperty -Name $key -Value $value.Trim() | |
} | |
else { | |
Write-Output $object | |
$object = New-Object -TypeName PSCustomObject | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment