Reputation: 13
I'm trying to make a PowerShell PSObject
out of the output of a batch file.
I am calling the batch file like this, which gives me an object containing the output of the batch file:
function browseGateway {
& $migrationUtilityScript "browse" "-z" "$sourceGateway" "-showIds" "--hideProgress" |
select -Skip 1 |
Tee-Object -Variable gatewayItems |
Out-Null |
Sort-Object
Clear-Host
return $gatewayItems
}
The output looks like this:
folder 27fa3a0eb01caf5f1e31b6aeadd68d35 _Management and internal
folder 2ab9e48a1b83968c3844d93bc0919919 Mediclinic
folder 2c0a788b7f39a0415b423f14c0aa9e9c OAuth
folder a49ab6404138110fbd6c993cc9b87e46 customers
folder b015056834707a42a07bcbfbbeb0a6dd Users
folder b015056834707a42a07bcbfbbeb2ca34 Product Offerings
folder e001cfd0c1c1ffaa18e187b5e72fc718 OTK-3.2.00
policy 20a437bb3db29c1700a4f074773ffa41 DemoLogo
service 2ab9e48a1b83968c3844d93bc08ba780 kevinGMU
service 2ab9e48a1b83968c3844d93bc09596e9 testJJ
service 3afaef13bd772e8e25acfe85c4d101a7 Error
service 88f089fba30183fc4f18b7b7d05f5889 test
I want to put them into PowerShell Objects for further processing:
I'm pretty sure you need to use New-Object
for this, but I'm not entirely sure how to sort the information. Do I need to use a regex for each word and split it up further?
Upvotes: 1
Views: 2526
Reputation: 3326
You can do this quite easily with some -split
s - regex is a tad bit overkill for something as simple as this.
$Content = (Invoke-WebRequest "http://pastebin.com/raw/591dk6Gj").Content
$FinalObj = $Content -split "`r`n" | % {
$Delimited = $_ -split "`t"
[PSCustomObject]@{
Col1 = $Delimited[0]
Col2 = $Delimited[1]
Col3 = $Delimited[2]
}
}
return $FinalObj
You end up with $FinalObj containing this:
Col1 Col2 Col3
---- ---- ----
folder 27fa3a0eb01caf5f1e31b6aeadd68d35 _Management and internal
folder 2ab9e48a1b83968c3844d93bc0919919 Mediclinic
folder 2c0a788b7f39a0415b423f14c0aa9e9c OAuth
folder a49ab6404138110fbd6c993cc9b87e46 customers
folder b015056834707a42a07bcbfbbeb0a6dd Users
folder b015056834707a42a07bcbfbbeb2ca34 Product Offerings
folder e001cfd0c1c1ffaa18e187b5e72fc718 OTK-3.2.00
policy 20a437bb3db29c1700a4f074773ffa41 DemoLogo
service 2ab9e48a1b83968c3844d93bc08ba780 kevinGMU
service 2ab9e48a1b83968c3844d93bc09596e9 testJJ
service 3afaef13bd772e8e25acfe85c4d101a7 Error
service 88f089fba30183fc4f18b7b7d05f5889 test
Upvotes: 0
Reputation: 6378
As you are effectively reading in a tab delimited file you don't need to use a RegEx, although you certainly could approach it that way. Once you have your raw input in a $items
object you can use the following statements to convert it into an array of PSObject
:
$items| ForEach-Object {
$properties = $_ -Split "`t"
New-Object PSObject -Property @{
Type = $properties[0].Trim();
ID = $properties[1].Trim();
Name = $properties[2].Trim();
}
}
Results in:
Name ID Type
---- -- ----
_Management and internal 27fa3a0eb01caf5f1e31b6aeadd68d35 folder
Mediclinic 2ab9e48a1b83968c3844d93bc0919919 folder
OAuth 2c0a788b7f39a0415b423f14c0aa9e9c folder
customers a49ab6404138110fbd6c993cc9b87e46 folder
Users b015056834707a42a07bcbfbbeb0a6dd folder
Product Offerings b015056834707a42a07bcbfbbeb2ca34 folder
OTK-3.2.00 e001cfd0c1c1ffaa18e187b5e72fc718 folder
DemoLogo 20a437bb3db29c1700a4f074773ffa41 policy
kevinGMU 2ab9e48a1b83968c3844d93bc08ba780 service
testJJ 2ab9e48a1b83968c3844d93bc09596e9 service
Error 3afaef13bd772e8e25acfe85c4d101a7 service
test 88f089fba30183fc4f18b7b7d05f5889 service
If you wanted to use a RegEx simply swap out the $_.Split
for an equivalent $_ -Match <RegEx>
and instead of referencing the $properties
array you would be referencing the $matches
variable. There is an exceptionally good run down of RegEx in PowerShell on Jouni Heikniemi's Blog.
Upvotes: 2