Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何获取只有填充值的属性?_Powershell_Quest - Fatal编程技术网

Powershell 如何获取只有填充值的属性?

Powershell 如何获取只有填充值的属性?,powershell,quest,Powershell,Quest,如何获取只有填充值的属性 例如,如果我跑步 Get-QADUser -Identity "SomeOne" -IncludeAllProperties 输出当然包括。。所有属性,包括有值和无值的属性。我想要一个只包含值的属性列表。一般来说是怎么做的 这不会局限于Quest cmdlet,我只使用Get-QADUser作为示例。首先使用Get-QADUser获取其属性(因为Get-QADUser依赖于AD模式,属性列表是动态的),然后过滤掉其定义中没有\{.*}的属性(也就是说,它们不是“get

如何获取只有填充值的属性

例如,如果我跑步

Get-QADUser -Identity "SomeOne" -IncludeAllProperties
输出当然包括。。所有属性,包括有值和无值的属性。我想要一个只包含值的属性列表。一般来说是怎么做的


这不会局限于Quest cmdlet,我只使用
Get-QADUser
作为示例。

首先使用
Get-QADUser
获取其属性(因为
Get-QADUser
依赖于AD模式,属性列表是动态的),然后过滤掉其定义中没有
\{.*}
的属性(也就是说,它们不是“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
…一个流程可以有许多属性(属性),有值或无值。要仅获取具有值的属性,请执行以下操作:

(Get-Process | Select -First 1).PSObject.Properties | ?{$_.Value -ne $null} | FT Name,Value
请注意,我将此限制为仅由
Get process
返回的第一个进程。然后,我们获取该对象上定义的所有属性,在Value不为null的位置进行过滤,然后仅显示这些属性的名称和

下面是方便功能删除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

删除空属性
源代码:

<#
.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
  }

}
[pscustomobject]@{one=1;two=null;three=3}删除NullProperties
13
--- -----
1     3
#>
函数删除空属性{
param(
[参数(必需,ValueFromPipeline)]
[psobject]$InputObject
)
过程{
#创建初始为空的输出对象
$obj=[pscustomobject]::new()
#循环所有输入对象属性。
foreach($InputObject.psobject.properties中的prop){
#如果属性为非-$null,请将其添加到输出对象。
if($null-ne$InputObject.$($prop.Name)){
添加成员-InputObject$obj-NotePropertyName$prop.Name-NotePropertyValue$prop.Value
}
}
#为输出对象指定一个反映输入类型的类型名
#对象前缀为“NonNull”-请注意,这纯粹是信息性的,除非
#您可以为此类型名称定义自定义输出格式。
$obj.pstypenames.Insert(0,'NonNull.+$InputObject.GetType().FullName)
#输出输出对象。
$obj
}
}

从Infoblox csv文件导入对象时,这些答案对我不起作用。有些值是空字符串,但不是null。测试属性是否为真似乎对我更有效。结果是一个对象

$a = [pscustomobject]@{one='hi';two='';three='there'}
$prop = $a.psobject.Properties | where value | foreach name
$a | select $prop

one three
--- -----
hi  there

这是一个更好的方法:)@Vesper:具体来说,这个答案的方法更简洁,性能更好(因为不需要
Get Member
调用),并且隐式地还包括
NoteProperty
成员。
.psobject.properties
方法还可以更容易地排除(很少见到)纯写属性:
?{$\u.IsGettable-和$\u.Value-ne$null}
因为尝试排除纯写属性而受到赞誉,尽管在PowerShell上下文中遇到它们的可能性很小
-match'\{get;'
就可以了。如果您想将其概括为包含
NoteProperty
成员,请使用
-Type属性
(复数),但是
-match
测试不起作用(也不需要)。为了培养良好的习惯,我可以建议您使用类似
$member.name+':“+$someone[$member.name]
是否作为输出语句?