简体   繁体   English

Powershell-过滤命令输出

[英]Powershell - filtering output of command

I'm quite new to powershell, but I've done a lot of batch scripting (yay for moving into the 'now!'). 我对Powershell还是很陌生,但是我已经做了大量的批处理脚本编写(是为了进入“现在!”)。 I'm trying to re-write my largest accomplishment in batch scripting into powershell, and right off the bat I'm hitting a bit of a wall. 我试图将我在批处理脚本方面的最大成就重新写到powershell中,而马上我碰到了一点困难。

What my original batch script did was install drivers for all of the detected system hardware. 我最初的批处理脚本所做的是为所有检测到的系统硬件安装驱动程序。 It did this by running devcon.exe and doing a search on the output, looking for VEN_ &DEV_ and trying to match it up with a comparison. 它通过运行devcon.exe并在输出中进行搜索,查找VEN_&DEV_并尝试将其与比较进行匹配来做到这一点。 This took a bit of time on slower computers (i3/Atom/slow AMD). 在速度较慢的计算机(i3 / Atom /速度较慢的AMD)上,这花费了一些时间。

I stumbled across this command in powershell: get-wmiobject -class CIM_VideoController PNPDeviceID 我在Powershell中偶然发现了以下命令:get-wmiobject -class CIM_VideoController PNPDeviceID

It spits out a list which contains just a few bits of info on the display adapter. 它弹出一个列表,其中仅包含显示适配器上的一些信息。 The line in particular I'd like is the PNPDeviceID. 我特别想要的行是PNPDeviceID。 I so far haven't had much luck in finding a way to manupulate the output to list just the VEN_ numbers. 到目前为止,我还没有找到一种方法来操纵输出以仅列出VEN_数字,因此运气不佳。 Here's what I'd like to do: Run the command above, manipulate it so I get just the vendor number into one variable and the device number into another variable. 这是我想做的:运行上面的命令,对其进行操作,以便仅将供应商编号放入一个变量,将设备编号放入另一个变量。 I tried doing this: 我尝试这样做:

get-wmiobject -class CIM_VideoController PNPDeviceID | Select-String -Pattern "PNPDeviceID" -SimpleMatch

The problem I'm having is, it spits out nothing at all. 我遇到的问题是,它根本不会吐出任何东西。 I also have no clue on how to manipulate the output of that line further giving me only the 4 digit identifier of the 'VEN_' or the 'DEV_'. 我也对如何操纵该行的输出一无所知,进一步只给了我'VEN_'或'DEV_'的4位数字标识符。

Would anyone know how to do this? 有人知道该怎么做吗?

You can just do this: 您可以这样做:

$deviceID = (get-wmiobject -class CIM_VideoController).PNPDeviceID

the output of such objects are allways stored in a property which you can access by dot notation 这些对象的输出始终存储在一个属性中,您可以通过点表示法对其进行访问

I mean no disrespect, but this is pretty basic stuff. 我的意思是不尊重,但这是非常基本的东西。 Have you considered finding a book (even an online one) and reading up on PowerShell? 您是否考虑过寻找一本书(甚至是在线书籍)并在PowerShell上阅读? I've heard good things about Learning PowerShell In A Month Of Lunches. 我听说过一个月的午餐学习PowerShell。

As for your request, to get the four digit ID you could pipe that property's value to a regex match, and then output that match. 对于您的请求,要获取四位数的ID,您可以将该属性的值通过管道传递给正则表达式匹配项,然后输出该匹配项。 It could be done like this: 可以这样完成:

$VidCardID = get-wmiobject -class CIM_VideoController PNPDeviceID | Where{$_.PNPDeviceID -match "VEN_(\d{4})"} | ForEach{$Matches[1]}

That will set $VidCardID to the 4 digit ID for the video card. 这会将$VidCardID设置为视频卡的4位ID。

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

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