当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 从管道中移除对象

作者:精品下载站 日期:2024-12-14 03:03:27 浏览:16 分类:玩电脑

从管道中移除对象


在 PowerShell 中,您经常生成并向管道传递比您想要的更多的对象。您可以使用 Format-* cmdlet 指定要显示的特定对象的属性,但这无助于解决从显示中删除整个对象的问题。您可能希望在管道结束之前过滤对象,以便您可以仅对最初生成的对象的子集执行操作。

PowerShell 包含一个 Where-Object cmdlet,它允许您测试管道中的每个对象,并且仅在满足特定测试条件时才将其沿管道传递。未通过测试的对象将从管道中删除。您提供测试条件作为 FilterScript 参数的值。

使用Where-Object 执行简单测试

FilterScript 的值是一个脚本块 - 用大括号 ({}) 括起来的一个或多个 PowerShell 命令 - 计算结果为 true 或 false。这些脚本块可能很简单,但创建它们需要了解另一个 PowerShell 概念:比较运算符。比较运算符比较出现在其每一侧的项目。比较运算符以连字符 (-) 开头,后跟名称。基本比较运算符几乎适用于任何类型的对象。更高级的比较运算符可能仅适用于文本或数组。

笔记

默认情况下,PowerShell 比较运算符不区分大小写。

出于分析考虑,<>= 等符号不用作比较运算符。相反,比较运算符由字母组成。下表列出了基本的比较运算符。

Comparison Operator Meaning Example (returns true) -eq is equal to 1 -eq 1 -ne isn't equal to 1 -ne 2 -lt Is less than 1 -lt 2 -le Is less than or equal to 1 -le 2 -gt Is greater than 2 -gt 1 -ge Is greater than or equal to 2 -ge 1 -like Is like (wildcard comparison for text) "file.doc" -like "f*.do?" -notlike isn't like (wildcard comparison for text) "file.doc" -notlike "p*.doc" -contains Contains 1,2,3 -contains 1 -notcontains doesn't contain 1,2,3 -notcontains 4

Where-Object 脚本块使用特殊变量 $_ 来引用管道中的当前对象。以下是其工作原理的示例。如果您有一个数字列表,并且只想返回小于 3 的数字,则可以使用 Where-Object 来过滤数字,方法是键入:

1,2,3,4 | Where-Object {$_ -lt 3}
1
2

基于对象属性的过滤

由于 $_ 引用当前管道对象,因此我们可以访问其属性以进行测试。

作为示例,我们可以查看 WMI 中的 Win32_SystemDriver 类。特定系统上可能有数百个系统驱动程序,但您可能只对一组特定的系统驱动程序感兴趣,例如正在运行的驱动程序。对于Win32_SystemDriver类,相关属性是State。您可以过滤系统驱动程序,通过键入以下内容仅选择正在运行的驱动程序:

Get-CimInstance -Class Win32_SystemDriver |
    Where-Object {$_.State -eq 'Running'}

这仍然会产生一个很长的列表。您可能还希望通过测试 StartMode 值来过滤以仅选择设置为自动启动的驱动程序:

Get-CimInstance -Class Win32_SystemDriver |
    Where-Object {$_.State -eq "Running"} |
    Where-Object {$_.StartMode -eq "Auto"}
DisplayName : RAS Asynchronous Media Driver
Name        : AsyncMac
State       : Running
Status      : OK
Started     : True

DisplayName : Audio Stub Driver
Name        : audstub
State       : Running
Status      : OK
Started     : True
...

这为我们提供了许多不再需要的信息,因为我们知道驱动程序正在运行。事实上,此时我们可能需要的唯一信息是名称和显示名称。以下命令仅包含这两个属性,从而产生更简单的输出:

Get-CimInstance -Class Win32_SystemDriver |
    Where-Object {$_.State -eq "Running"} |
    Where-Object {$_.StartMode -eq "Manual"} |
    Format-Table -Property Name,DisplayName
Name              DisplayName
----              -----------
AsyncMac               RAS Asynchronous Media Driver
bindflt                Windows Bind Filter Driver
bowser                 Browser
CompositeBus           Composite Bus Enumerator Driver
condrv                 Console Driver
HdAudAddService        Microsoft 1.1 UAA Function Driver for High Definition Audio Service
HDAudBus               Microsoft UAA Bus Driver for High Definition Audio
HidUsb                 Microsoft HID Class Driver
HTTP                   HTTP Service
igfx                   igfx
IntcDAud               Intel(R) Display Audio
intelppm               Intel Processor Driver
...

上述命令中有两个 Where-Object 元素,但可以使用 -and 逻辑将它们表达在单个 Where-Object 元素中运算符,像这样:

Get-CimInstance -Class Win32_SystemDriver |
    Where-Object {($_.State -eq 'Running') -and ($_.StartMode -eq 'Manual')} |
    Format-Table -Property Name,DisplayName

下表列出了标准逻辑运算符。

Logical Operator Meaning Example (returns true) -and Logical and; true if both sides are true (1 -eq 1) -and (2 -eq 2) -or Logical or; true if either side is true (1 -eq 1) -or (1 -eq 2) -not Logical not; reverses true and false -not (1 -eq 2) ! Logical not; reverses true and false !(1 -eq 2)

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯