How do you programmatically get a list of all common parameters?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 09:02:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!