Private/NestedFunctions/Hide-ObjectProperty.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<#
.DESCRIPTION
This function will digest any object and set configure wether or not its properties will show by default when you call it.
.EXAMPLE
In this example you will need to replace the "$HiddenProperties" array and object name "$SomeObject".
$ObjectOutput += [PSCustomObject]@{
    'Var1' = 'Hello;
    'Var2' = 'World';
    'Var3' = '!';
}
# List values to add to results
$HiddenProperties = @('Var2','Var3')
# Append meta info to results
Get-Variable -Name:($HiddenProperties) |
    ForEach-Object {
    $Variable = $_
    $SomeObject |
        ForEach-Object {
        Add-Member -InputObject:($_) -MemberType:('NoteProperty') -Name:($Variable.Name) -Value:($Variable.Value)
    }
}
# Set the meta info to be hidden by default
$Results += Hide-ObjectProperty -Object:($Result) -HiddenProperties:($HiddenProperties)
#>

Function Hide-ObjectProperty
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0)][ValidateNotNullOrEmpty()][Object]$Object,
        [Parameter(Mandatory = $true, Position = 1)][ValidateNotNullOrEmpty()][Array]$HiddenProperties
    )
    # Set some properties to be hidden in the results
    $Object | ForEach-Object {
        # Get current object's properties
        $ObjectAllProperties = $_.PSObject.Properties.Name
        # Write-Host ('ObjectAllProperties:' + ($ObjectAllProperties -join ', ')) -BackgroundColor Gray -ForegroundColor Black
        # If current object has PSStandardMembers then get it's default set and add its hidden properties to PropertiesToHide
        If ($_.PSStandardMembers)
        {
            $ObjectShowProperties = $_.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
            $PropertiesToHide = @($HiddenProperties + ($ObjectAllProperties | Where-Object {$_ -notin $ObjectShowProperties -or $_ -in $HiddenProperties}) | Select-Object -Unique)
            # Write-Host ('ObjectShowProperties:' + ($ObjectShowProperties -join ', ')) -BackgroundColor Yellow -ForegroundColor Black
        }
        Else
        {
            $PropertiesToHide = @($HiddenProperties)
        }
        # Get list of properties to show
        $PropertiesToShow = $ObjectAllProperties | Where-Object {$_ -notin $PropertiesToHide}
        # Write-Host ('PropertiesToHide:' + ($PropertiesToHide -join ', ')) -BackgroundColor Yellow -ForegroundColor Black
        # Write-Host ('PropertiesToShow:' + ($PropertiesToShow -join ', ')) -BackgroundColor Green -ForegroundColor Black
        # Create the default property display set
        If ($PropertiesToShow)
        {
            $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet', [System.String[]]$PropertiesToShow)
            $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
            # Add the list of standard members
            Add-Member -InputObject:($_) -MemberType:('MemberSet') -Name:('PSStandardMembers') -Value:($PSStandardMembers) -Force
        }
        Else
        {
            Throw ('By hiding "' + ($PropertiesToHide -join '", "') + '" there are no properties to show. At least one property must be visitable.')
        }
    }
    If ($Object)
    {
        Return $Object
    }
}