Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse/pipe cmd line output using Powershell to an object

I have an *.exe that outputs this data when I run this PowerShell command:

& $myExe list

Where $myExe is something like C:\Temp\MyExe.exe and list is an argument.

List of Runbook ID on the system: 



List of services installed on the system: 

ALMService   Version: 7.0.4542.16189
AOSService   Version: 7.0.4542.16189
BIService    Version: 7.0.4542.16189
DevToolsService  Version: 7.0.4542.16189
DIXFService  Version: 7.0.4542.16189
MROneBox     Version: 7.1.1541.3036
PayrollTaxModule     Version: 7.1.1541.3036
PerfSDK  Version: 7.0.4542.16189
ReportingService     Version: 7.0.4542.16189
RetailCloudPos   Version: 7.1.1541.3036
RetailHQConfiguration    Version: 7.1.1541.3036
RetailSDK    Version: 7.1.1541.3036
RetailSelfService    Version: 7.1.1541.3036
RetailServer     Version: 7.1.1541.3036
RetailStorefront     Version: 7.1.1541.3036
SCMSelfService   Version: 7.1.1541.3036

The data I'm looking for is the first column of the table, but it has things like List of Runbook ID... at the top. Is there a good way in PowerShell to parse this data so I can get just the table data?

like image 789
Alex Kwitny Avatar asked Dec 14 '22 21:12

Alex Kwitny


1 Answers

You could save the output in a variable, use Where-Object to filter just the lines that have Version in it, then remove all the unwanted characters with a -replace regex.

$myExeOutput = & $myExe list
$myExeOutput |
    Where-Object {$_ -match 'Version:'} |
    ForEach-Object {
        $_ -replace '\s+Version:.*$',''
    }
like image 149
BenH Avatar answered May 12 '23 10:05

BenH