我正在尝试从批处理文件的输出中创建PowerShell PSObject
。
我正在调用这样的批处理文件,它给了我一个包含批处理文件输出的对象:
function browseGateway {
& $migrationUtilityScript "browse" "-z" "$sourceGateway" "-showIds" "--hideProgress" |
select -Skip 1 |
Tee-Object -Variable gatewayItems |
Out-Null |
Sort-Object
Clear-Host
return $gatewayItems
}
输出如下:
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
我想将它们放入PowerShell对象中进行进一步处理:
我很确定你需要使用New-Object
,但我不完全确定如何对信息进行排序。我是否需要为每个单词使用正则表达式并将其进一步拆分?
答案 0 :(得分:2)
由于您在制表符分隔文件中有效阅读,因此您无需使用RegEx,尽管您当然可以采用这种方式。在$items
对象中输入原始输入后,可以使用以下语句将其转换为PSObject
数组:
$items| ForEach-Object {
$properties = $_ -Split "`t"
New-Object PSObject -Property @{
Type = $properties[0].Trim();
ID = $properties[1].Trim();
Name = $properties[2].Trim();
}
}
结果:
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
如果您想使用RegEx,只需将$_.Split
换成等效的$_ -Match <RegEx>
,而不是引用$properties
数组,而是引用$matches
变量。在Jouni Heikniemi博客上有一个exceptionally good run down of RegEx in PowerShell。
答案 1 :(得分:0)
你可以用一些-split
s很容易地做到这一点 - 对于像这样简单的东西,正则表达式有点过分。
$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
你最终得到的$ FinalObj包含:
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