In the previous tip we explained how you can dump all the legal values for a PowerShell attribute. Today we’ll take a look at the [Parameter()] attribute and its value DontShow. Take a look at this function: 在前一个技能中我们介绍了如何导出一个 PowerShell 属性的所有合法值。今天我们将关注 [Parameter()] 属性和它的值 DontShow。我们来看看这个函数:
dynamicparam { # this hash table defines the departments available in each company $data = @{ Microsoft = 'CEO', 'Marketing', 'Delivery' Google = 'Marketing', 'Delivery' Amazon = 'CEO', 'IT', 'Carpool' Facebook = 'CEO', 'Facility', 'Carpool' }
# check to see whether the user already chose a company if ($Company) { # yes, so create a new dynamic parameter $paramDictionary = New-Object-TypeName System.Management.Automation.RuntimeDefinedParameterDictionary $attributeCollection = New-Object-TypeName System.Collections.ObjectModel.Collection[System.Attribute]
# create the appropriate ValidateSet attribute, listing the legal values for # this dynamic parameter $attribute = New-Object System.Management.Automation.ValidateSetAttribute($data.$Company) $attributeCollection.Add($attribute)
process { # if the user entered a string, get the real object if ($PSCmdlet.ParameterSetName -eq'String') { $Service = Get-Service-Name$Name } else { # else, if the user entered (piped) the expected object in the first place, # you are good to go }
# this call tells you which parameter set was invoked $PSCmdlet.ParameterSetName
# take any object, and dump a list of its properties Get-Process-Id$pid | Get-Member-MemberType *property | Select-Object-ExpandProperty Name | Sort-Object