简体   繁体   English

如何使用PowerShell解析Windows命令`wmic`的output

[英]How to parse the output of the Windows command `wmic` using PowerShell

I am trying to parse the output of:我正在尝试解析 output 的:

wmic copmutersystem

and

net.exe config workstation

using PowerShell to get an object as Key/Value pair and/or convert it to JSON String.使用 PowerShell 获取 object 作为键/值对和/或将其转换为 JSON 字符串。

I know there is a PowerShell equivalent command:我知道有一个 PowerShell 等效命令:

Get-CimInstance  -Class Win32_computersystem

But the ask here is to figure out how to use PowerShell to parse a similar output for wmic CMD line.但是这里的问题是弄清楚如何使用 PowerShell 为wmic CMD 行解析类似的 output。

Use the Get-CimInstance and ConvertTo-Json commandlets:使用Get-CimInstanceConvertTo-Json命令行开关:

Get-CimInstance -Class Win32_ComputerSystem | ConvertTo-Json

Edit: Previous revision of this answer used Get-WMIObject , but that's been deprecated .编辑:此答案的先前版本使用了Get-WMIObject ,但已被弃用

Wmic can output csv or xml, but obviously get-wmiobject or get-ciminstance is preferred. wmic可以output csv或者xml,但是显然get-wmiobject或者get-ciminstance是首选。 You just need to find the class names instead of the aliases.您只需要找到 class 名称而不是别名。 The creator of wmic and powershell is the same. wmic的创建者和powershell是同一个。

wmic computersystem list /format:csv | convertfrom-csv | select model

Model
-----
OptiPlex 7490 AIO

List wmic class aliases:列出 wmic class 别名:

wmic alias list brief
wmic alias where "friendlyname = 'computersystem'" list brief
wmic alias where "friendlyname like '%comp%'" list brief

FriendlyName    PWhere  Target
ComputerSystem          Select * from Win32_ComputerSystem

For example:例如:

ComputerSystem                                   Select * from Win32_ComputerSystem
get-ciminstance win32_computersystem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM