问题
Powershell Cmdlets inherit a bunch of common parameters. Some cmdlets I write end up with predicates that depend on which parameters are actually bound. This often leads to filtering out common parameters which means you need a list of common parameter names.
I also expect there to be difference in the list of common parameters from one version of powershell to another.
All of this boils down to this question:
How do you programmatically determine the list of common parameters?
回答1:
What about these static properties?
[System.Management.Automation.PSCmdlet]::CommonParameters
[System.Management.Automation.PSCmdlet]::OptionalCommonParameters
The existing common parameters is the combination of both lists:
CommonParameters: Lists the common parameters that are added by the PowerShell engine to any cmdlet that derives from PSCmdlet.
OptionalCommonParameters: Lists the common parameters that are added by the PowerShell engine when a cmdlet defines additional capabilities (SupportsShouldProcess, SupportsTransactions)
i.e. All of them can exist, but the optional ones only exists if the cmdlet supports them. For detailed info see Cmdlet Class
回答2:
Like this:
function Get-CommonParameterNames
{
[CmdletBinding()]
param()
$MyInvocation.MyCommand.Parameters.Keys
}
来源:https://stackoverflow.com/questions/29956201/how-do-you-programmatically-get-a-list-of-all-common-parameters