How to exclude non-valued object properties when converting to JSON in Powershell

后端 未结 4 1905
独厮守ぢ
独厮守ぢ 2020-12-31 10:40

I have a piece of code that works but I want to know if there is a better way to do it. I could not find anything related so far. Here are the facts:

  • I have an
4条回答
  •  情书的邮戳
    2020-12-31 11:18

    I made my own modified version of batmanama's answer that accepts an additional parameter, letting you remove elements that are also present in the list present in that parameter. For example:

    Get-CimInstance -ClassName Win32_UserProfile | 
    Remove-Null -AlsoRemove 'Win32_FolderRedirectionHealth' | Format-Table
    

    I've posted a gist version including PowerShell documentation as well.

    Function Remove-Null {
        [CmdletBinding()]
        Param(
            # Object from which to remove the null values.
            [Parameter(ValueFromPipeline,Mandatory)]
            $InputObject,
            # Instead of also removing values that are empty strings, include them
            # in the output.
            [Switch]$LeaveEmptyStrings,
            # Additional entries to remove, which are either present in the
            # properties list as an object or as a string representation of the
            # object.
            # I.e. $item.ToString().
            [Object[]]$AlsoRemove = @()
        )
        Process {
            # Iterate InputObject in case input was passed as an array
            ForEach ($obj in $InputObject) {
                $obj | Select-Object -Property (
                    $obj.PSObject.Properties.Name | Where-Object {
                        -not (
                            # If prop is null, Exclude
                            $null -eq $obj.$_ -or
                            # If -LeaveEmptyStrings is specified and the property
                            # is an empty string, Exclude
                            ($LeaveEmptyStrings.IsPresent -and
                                [string]::IsNullOrEmpty($obj.$_)) -or
                            # If AlsoRemove contains the property, Exclude
                            $AlsoRemove.Contains($obj.$_) -or
                            # If AlsoRemove contains the string representation of
                            # the property, Exclude
                            $AlsoRemove.Contains($obj.$_.ToString())
                        )
                    }
                )
            }
        }
    }
    

    Note that the process block here automatically iterates a pipeline object, so the ForEach will only iterate more than once when an item is either explicitly passed in an array—such as by wrapping it in a single element array ,$array—or when provided as a direct argument, such as Remove-Null -InputObject $(Get-ChildItem).

    It's also worth mentioning that both mine and batmanama's functions will remove these properties from each individual object. That is how it can properly utilize the PowerShell pipeline. Furthermore, that means that if any of the objects in the InputObject have a property that does not match (e.g. they are not null), an output table will still show that property, even though it has removed those properties from other items that did match.

    Here's a simple example showing that behavior:

    @([pscustomobject]@{Number=1;Bool=$true};
    [pscustomobject]@{Number=2;Bool=$false},
    [pscustomobject]@{Number=3;Bool=$true},
    [pscustomobject]@{Number=4;Bool=$false}) | Remove-Null -AlsoRemove $false
    
    Number Bool
    ------ ----
         1 True
         2
         3 True
         4
    

提交回复
热议问题