如何获得只有填充值的属性?如何获得只有填充值的属性?
因此,举例来说,如果我跑
Get-QADUser -Identity "SomeOne" -IncludeAllProperties
过程的输出将包括..所有的属性,包括那些和那些没有价值。我想要一个只有值的属性列表。一般来说如何做?
这不会仅限于Quest Cmdlet,我仅以Get-QADUser
为例。
如何获得只有填充值的属性?如何获得只有填充值的属性?
因此,举例来说,如果我跑
Get-QADUser -Identity "SomeOne" -IncludeAllProperties
过程的输出将包括..所有的属性,包括那些和那些没有价值。我想要一个只有值的属性列表。一般来说如何做?
这不会仅限于Quest Cmdlet,我仅以Get-QADUser
为例。
你可以尝试使用PowerShell中的内置(隐藏)属性的对象称为PSObject,其中包括一个名为属性财产,即一个父对象上的所有属性的列表。
可能更容易一个例子。以Get-Process
...一个进程可以有许多属性(属性),有或没有值。为了与价值观得到只是那些你这样做:
(Get-Process | Select -First 1).PSObject.Properties | ?{$_.Value -ne $null} | FT Name,Value
注意,我限于这只是Get-Process
返回的第一个过程。然后,我们获取在该对象上定义的所有属性,筛选其中值不为空,然后显示名称和值对于这些属性。
你第一次得到它的属性(因为Get-QADUser
取决于AD架构,属性列表是动态的)与get-member -type property
,然后过滤掉那些没有\{.*(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]
}
}
虽然尝试排除只写属性,但在PowerShell上下文中运行的机会很渺茫, ' - 匹配'\ {get;''会做。 如果你想概括这个以包含'NoteProperty'成员,使用'-Type Properties'(复数),但是'-match'测试不会工作(并且不需要)。 为了促进良好的习惯,我可以建议你[避免使用'Write-Host'](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/)和使用'$ member.name +'之类的东西:'+ $ someone [$ member.name]'作为输出语句? – mklement0
要补Charlie Joynt's helpful answer:
下面是便利功能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
}
}
Now ... That很好:) –
说实话,你们都帮了很多忙。很多谢谢! –
从Infoblox csv文件导入对象时,这些答案对我无效。有些值是空字符串,但不是空值。测试一个属性是否为真,似乎对我更好。结果是一个对象。
$a = [pscustomobject]@{one='hi';two='';three='there'}
$prop = $a.psobject.Properties | where value | select -expand name
$a | select $prop
one three
--- -----
hi there
这是一个更好的方法:) – Vesper
@Vesper:具体来说,这个答案的方法更简洁,性能更好(因为不需要'Get-Member'调用),并隐含地包含'NoteProperty'成员。 '.psobject.properties'方法还可以使排除(罕见)只写属性变得更容易:'?{$ _。IsGettable -and $ _。Value -ne $ null}' – mklement0