未加星标

Filtering of Objects and Properties

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Saw a post on the forum today that suggests people are still confused about how to perform filtering of objects and properties in PowerShell.

As with so much in PowerShell explanations are always better with examples.

Let’s start with the physical disks in a computer:

PS> Get-PhysicalDisk FriendlyName SerialNumber CanPool OperationalStatus HealthStatus Usage Size ------------ ------------ ------- ----------------- ------------ ----- ---- Toshiba USB 2.0 Ext. HDD WD-WCAMR3209671 False OK Healthy Auto-Select 298.09 GB ST916082 1A DEF10E8D9B36 False OK Healthy Auto-Select 149.05 GB Samsung SSD 840 PRO Series S1AXNSAF329511V False OK Healthy Auto-Select 476.94 GB

If you want to objects that match a specific criteria for instance disk is larger than 300GB:

PS> Get-PhysicalDisk | Where-Object Size -gt 300GB FriendlyName SerialNumber CanPool OperationalStatus HealthStatus Usage Size ------------ ------------ ------- ----------------- ------------ ----- ---- Samsung SSD 840 PRO Series S1AXNSAF329511V False OK Healthy Auto-Select 476.94 GB

Where-Object is your friend.

What you’re actually doing though very few people actually write it like this is

Get-PhysicalDisk | Where-Object -Property Size -GT -Value 300GB

The help file for Where-Object lists the possible operators.

You can also show the original style syntax

Get-PhysicalDisk | Where-Object -FilterScript {$_.Size -gt 300GB}

Normal usage is to not write the FilterScript parameter so it becomes

Get-PhysicalDisk | Where-Object {$_.Size -gt 300GB}

$_ represents the object currently on the pipeline. If you need to use multiply conditions in your filter you’ll need to use the older style syntax.

So far you’ve seen how reduce the number of objects on the pipeline. Where-Object filters out those that don’t match the given criteria.

If you want to reduce the number of properties that the objects on the pipeline possess you’ll need to use Select-Object

PS> Get-PhysicalDisk | Select-Object -Property FriendlyName, HealthStatus, Size FriendlyName HealthStatus Size ------------ ------------ ---- Toshiba USB 2.0 Ext. HDD Healthy 320072933376 ST916082 1A Healthy 160041885696 Samsung SSD 840 PRO Series Healthy 512110190592

More commonly written as

Get-PhysicalDisk | Select FriendlyName, HealthStatus, Size

PowerShell best practice is always to use the full cmdlet and parameter names in your scripts. The *-Object cmdlets and in particular Where-Object, Sort-Object and Select-Object are often abbreviated to Where, Sort and Select and the parameters only used where necessary. This was the way I was advised to use them by Jeffrey Snover who invented PowerShell when I wrote PowerShell in Practice. Good enough for me.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShell
分页:12
转载请注明
本文标题:Filtering of Objects and Properties
本站链接:http://www.codesec.net/view/535145.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(15)