gpt4 book ai didi

powershell - 如何使用 Powershell 解析/管道命令行输出到对象

转载 作者:行者123 更新时间:2023-12-03 00:23:12 24 4
gpt4 key购买 nike

当我运行此 PowerShell 命令时,我有一个 *.exe 输出此数据:
& $myExe list
哪里$myExe类似于 C:\Temp\MyExe.exelist是一个论点。

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

我正在寻找的数据是表的第一列,但它有类似 List of Runbook ID... 的内容。在顶部。 PowerShell 中是否有解析这些数据的好方法,以便我可以只获取表数据?

最佳答案

您可以将输出保存在变量中,使用 Where-Object仅过滤具有 Version 的行在其中,然后使用 -replace 删除所有不需要的字符正则表达式。

$myExeOutput = & $myExe list
$myExeOutput |
Where-Object {$_ -match 'Version:'} |
ForEach-Object {
$_ -replace '\s+Version:.*$',''
}

关于powershell - 如何使用 Powershell 解析/管道命令行输出到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com