如何获取仅具有填充值的属性?
How do I get properties that ONLY have populated values?
如何获取只有填充值的属性?
例如,如果我 运行
Get-QADUser -Identity "SomeOne" -IncludeAllProperties
输出当然会包括..所有属性,包括有值和无值的属性。我想要一个只有值的属性列表。一般是怎么做的?
这不会仅限于 Quest Cmdlet,我仅以 Get-QADUser
为例。
你先用get-member -type property
得到它的属性(因为Get-QADUser
依赖于AD架构,属性列表是动态的),然后过滤掉那些没有\{.*(get).*\}
的它的定义(也就是说,它们不是 "gettable"),然后按名称枚举结果列表并过滤掉空值。
$someone=Get-QADUser -Identity "SomeOne" -IncludeAllProperties
$members=$someone|get-member -type property| where {$_.definition -match '\{.*(get).*\}'}
foreach ($member in $members) {
if ($someone[$member.name] -ne $null) {
write-host $member.name $someone[$member.name]
}
}
您可以尝试使用名为 PSObject 的 PowerShell 对象的内置(隐藏)属性,其中包括一个名为 的 属性 ]Properties,即父对象的所有属性列表。
举个例子可能更容易。就拿Get-Process
... 一个进程可以有很多属性(property)有或没有值。为了只获得具有值的值,您可以这样做:
(Get-Process | Select -First 1).PSObject.Properties | ?{$_.Value -ne $null} | FT Name,Value
请注意,我将其限制为 Get-Process
返回的第一个进程。然后,我们获取该对象上定义的所有属性,过滤 Value 不为 null 的位置,然后仅显示 Name 和 Value 对于这些属性。
要补充 :
下面是 便利函数 Remove-NullProperties
,它创建其输入对象的自定义对象副本,仅填充输入的非 $null
属性对象。
使用示例:
# Sample input collection, with 2 objects with different $null-valued
# properties.
$coll = [pscustomobject] @{ one = 'r1c1'; two = $null; three = 'r1c3' },
[pscustomobject] @{ one = 'r2c1'; two = 'r2c2'; three = $null }
# Output copies containing only non-$null-valued properties.
# NOTE: The `ForEach-Object { Out-String -InputObject $_ }` part is solely
# there to ensure that *all* resulting properties are shown.
# With the default output, only the properties found on the FIRST
# input object would be used in the output table.
$coll | Remove-NullProperties |
ForEach-Object { Out-String -InputObject $_ }
这会产生以下结果 - 请注意如何删除相应的空值属性:
one three
--- -----
r1c1 r1c3
one two
--- ---
r2c1 r2c2
Remove-NullProperties
源代码:
<#
.SYNOPSIS
Removes properties with $null values from custom-object copies of
the input objects.
.DESCRIPTION
Note that output objects are custom objects that are copies of the input
objects with copies of only those input-object properties that are not $null.
CAVEAT: If you pipe multiple objects to this function, and these objects
differ in what properties are non-$null-valued, the default output
format will show only the non-$null-valued properties of the FIRST object.
Use ... | ForEach-Object { Out-String -InputObject $_ } to avoid
this problem.
.NOTES
Since the output objects are generally of a distinct type - [pscustomobject] -
and have only NoteProperty members, use of this function only makes sense
with plain-old data objects as input.
.EXAMPLE
> [pscustomobject] @{ one = 1; two = $null; three = 3 } | Remove-NullProperties
one three
--- -----
1 3
#>
function Remove-NullProperties {
param(
[parameter(Mandatory,ValueFromPipeline)]
[psobject] $InputObject
)
process {
# Create the initially empty output object
$obj = [pscustomobject]::new()
# Loop over all input-object properties.
foreach($prop in $InputObject.psobject.properties) {
# If a property is non-$null, add it to the output object.
if ($null -ne $InputObject.$($prop.Name)) {
Add-Member -InputObject $obj -NotePropertyName $prop.Name -NotePropertyValue $prop.Value
}
}
# Give the output object a type name that reflects the type of the input
# object prefixed with 'NonNull.' - note that this is purely informational, unless
# you define a custom output format for this type name.
$obj.pstypenames.Insert(0, 'NonNull.' + $InputObject.GetType().FullName)
# Output the output object.
$obj
}
}
在从 Infoblox csv 文件导入对象的情况下,这些答案对我不起作用。有些值是空字符串,但不为空。测试 属性 是否为真对我来说似乎更有效。结果是一个对象。
$a = [pscustomobject]@{one='hi';two='';three='there'}
$prop = $a.psobject.Properties | where value | foreach name
$a | select $prop
one three
--- -----
hi there
如何获取只有填充值的属性?
例如,如果我 运行
Get-QADUser -Identity "SomeOne" -IncludeAllProperties
输出当然会包括..所有属性,包括有值和无值的属性。我想要一个只有值的属性列表。一般是怎么做的?
这不会仅限于 Quest Cmdlet,我仅以 Get-QADUser
为例。
你先用get-member -type property
得到它的属性(因为Get-QADUser
依赖于AD架构,属性列表是动态的),然后过滤掉那些没有\{.*(get).*\}
的它的定义(也就是说,它们不是 "gettable"),然后按名称枚举结果列表并过滤掉空值。
$someone=Get-QADUser -Identity "SomeOne" -IncludeAllProperties
$members=$someone|get-member -type property| where {$_.definition -match '\{.*(get).*\}'}
foreach ($member in $members) {
if ($someone[$member.name] -ne $null) {
write-host $member.name $someone[$member.name]
}
}
您可以尝试使用名为 PSObject 的 PowerShell 对象的内置(隐藏)属性,其中包括一个名为 的 属性 ]Properties,即父对象的所有属性列表。
举个例子可能更容易。就拿Get-Process
... 一个进程可以有很多属性(property)有或没有值。为了只获得具有值的值,您可以这样做:
(Get-Process | Select -First 1).PSObject.Properties | ?{$_.Value -ne $null} | FT Name,Value
请注意,我将其限制为 Get-Process
返回的第一个进程。然后,我们获取该对象上定义的所有属性,过滤 Value 不为 null 的位置,然后仅显示 Name 和 Value 对于这些属性。
要补充
下面是 便利函数 Remove-NullProperties
,它创建其输入对象的自定义对象副本,仅填充输入的非 $null
属性对象。
使用示例:
# Sample input collection, with 2 objects with different $null-valued
# properties.
$coll = [pscustomobject] @{ one = 'r1c1'; two = $null; three = 'r1c3' },
[pscustomobject] @{ one = 'r2c1'; two = 'r2c2'; three = $null }
# Output copies containing only non-$null-valued properties.
# NOTE: The `ForEach-Object { Out-String -InputObject $_ }` part is solely
# there to ensure that *all* resulting properties are shown.
# With the default output, only the properties found on the FIRST
# input object would be used in the output table.
$coll | Remove-NullProperties |
ForEach-Object { Out-String -InputObject $_ }
这会产生以下结果 - 请注意如何删除相应的空值属性:
one three
--- -----
r1c1 r1c3
one two
--- ---
r2c1 r2c2
Remove-NullProperties
源代码:
<#
.SYNOPSIS
Removes properties with $null values from custom-object copies of
the input objects.
.DESCRIPTION
Note that output objects are custom objects that are copies of the input
objects with copies of only those input-object properties that are not $null.
CAVEAT: If you pipe multiple objects to this function, and these objects
differ in what properties are non-$null-valued, the default output
format will show only the non-$null-valued properties of the FIRST object.
Use ... | ForEach-Object { Out-String -InputObject $_ } to avoid
this problem.
.NOTES
Since the output objects are generally of a distinct type - [pscustomobject] -
and have only NoteProperty members, use of this function only makes sense
with plain-old data objects as input.
.EXAMPLE
> [pscustomobject] @{ one = 1; two = $null; three = 3 } | Remove-NullProperties
one three
--- -----
1 3
#>
function Remove-NullProperties {
param(
[parameter(Mandatory,ValueFromPipeline)]
[psobject] $InputObject
)
process {
# Create the initially empty output object
$obj = [pscustomobject]::new()
# Loop over all input-object properties.
foreach($prop in $InputObject.psobject.properties) {
# If a property is non-$null, add it to the output object.
if ($null -ne $InputObject.$($prop.Name)) {
Add-Member -InputObject $obj -NotePropertyName $prop.Name -NotePropertyValue $prop.Value
}
}
# Give the output object a type name that reflects the type of the input
# object prefixed with 'NonNull.' - note that this is purely informational, unless
# you define a custom output format for this type name.
$obj.pstypenames.Insert(0, 'NonNull.' + $InputObject.GetType().FullName)
# Output the output object.
$obj
}
}
在从 Infoblox csv 文件导入对象的情况下,这些答案对我不起作用。有些值是空字符串,但不为空。测试 属性 是否为真对我来说似乎更有效。结果是一个对象。
$a = [pscustomobject]@{one='hi';two='';three='there'}
$prop = $a.psobject.Properties | where value | foreach name
$a | select $prop
one three
--- -----
hi there