PowerTip of the Day-Removing Empty Things

原文地址:http://app.en25.com/e/es.aspx?s=1403&e=4204&elq=c21eede5a82e4a9e99d852b591c961e5

原文:

 

Removing Empty Things

How do you exclude objects based on empty properties? For example, WMI returns all kinds of "network adapters:"

Get-WMIObject Win32_NetworkAdapterConfiguration

To focus only on those that have an IPAddress assigned, you should exclude any object that has an empty IPAddress property like this:

Get-WMIObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress }

That's all. Where-Object will automatically convert any null-property to $false and any other to $true. That's why this line will only display processes that have a non-null company property:

Get-Process | Where-Object { $_.Company } | Select-Object Name, Company

 

翻译:

排除空的东西

如何把属性为空的对象排除掉?比如,WMI通过下面的语句会列出所有的网络适配器:

Get-WMIObject Win32_NetworkAdapterConfiguration

如果想只关注那些已经赋予了IP地址的适配器,就需要排除掉IP地址属性为空的所有适配器,代码如下:

Get-WMIObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress }

搞定。Where-object会自动把空属性转化为$false,非空属性转化为$true

以下代码显示进程中company属性非空的所有进程:

Get-Process | Where-Object { $_.Company } | Select-Object Name, Company

 

 

 

笔记:

Get,Where,Select,怎么看都像LINQ.

本文主要复习的是根据条件过滤。

posted @ 2010-06-08 10:58  哥本哈士奇(aspnetx)  阅读(329)  评论(0编辑  收藏  举报