且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取仅具有填充值的属性?

更新时间:2023-11-26 13:09:16

您可以尝试使用名为 PSObject 的 PowerShell 对象的内置(隐藏)属性,其中包括名为 的属性属性,即父对象上所有属性的列表.

You could try using the built-in (hidden) property of PowerShell objects called PSObject, which includes a property called Properties, i.e. a list of all properties on the parent object.

举个例子也许更容易.以 Get-Process 为例,一个进程可以有许多带有或不带有值的属性(属性).为了只获得具有值的值,您可以这样做:

Maybe easier with an example. Take Get-Process... a process can have many attributes (properties) with or without values. In order to get just the ones with values you do this:

(Get-Process | Select -First 1).PSObject.Properties | ?{$_.Value -ne $null} | FT Name,Value

请注意,我仅将其限制为 Get-Process 返回的第一个进程.然后我们获取在该对象上定义的所有属性,过滤 Value 不为空的地方,然后只显示这些属性的 NameValue.

Note that I limited this to just the first process returned by Get-Process. We then get all the properties defined on that object, filtering where Value is not null and then displaying just the Name and Value for those properties.