The DontShow property

DontShow may be used to hide a parameter from tab completion and IntelliSense. This property is rarely used, but may be occasionally useful for short recursive functions. The following function recursively calls itself, comparing MaxDepth and CurrentDepth. The CurrentDepth parameter is owned by the function and a user is never expected to supply a value:

function Show-Property {
[CmdletBinding()]
param (
# Show the properties of the specified object.
[Parameter(Mandatory)]
[PSObject]$InputObject,

# The maximum depth when expanding properties of child objects.
[Int32]$MaxDepth = 5,

# Used to track the current depth during recursion.
[Parameter(DontShow)]
[Int32]$CurrentDepth = 0
)

$width = $InputObject.PSObject.Properties.Name |
Sort-Object { $_.Length } -Descending |
Select-Object -First 1 -ExpandProperty Length

foreach ($property in $InputObject.PSObject.Properties) {
'{0}{1}: {2}' -f
(' ' * $CurrentDepth),
$property.Name.PadRight($width, ' '),
$property.TypeNameOfValue

if ($CurrentDepth -lt $MaxDepth -and $property.Value -and
-not $property.TypeNameOfValue.IsPrimitive) {

Show-Property -InputObject $property.Value -CurrentDepth ($CurrentDepth + 1)
}
}
}

Marking a parameter as DontShow hides the parameter to a degree, but it does nothing to prevent a user from providing a value for the parameter. In this preceding case, a better approach might be to move the body of the function into a nested function. Alternatively, if the function is part of a module, the recursive code might be moved to a function that is not exported from a module and exposed by a second, tidier, function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.155