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

Powershell cmdlets inherit a bunch of common parameters . Some cmdlets that I write end with predicates that depend on which parameters are actually related. This often results in filtering common parameters, which means you need a list of common parameter names.

I also expect the difference in the list of general options from one version of powershell to another.

It all boils down to the following question:

How do you programmatically define a list of common parameters?

+6
source share
2 answers

What about these static properties?

[System.Management.Automation.PSCmdlet]::CommonParameters [System.Management.Automation.PSCmdlet]::OptionalCommonParameters 

The existing common parameters are a combination of both lists:

CommonParameters : Lists the common parameters added by the PowerShell engine to any cmdlet obtained from the PSCmdlet.

OptionalCommonParameters : lists the general parameters added by the PowerShell engine when the cmdlet defines additional features (SupportsShouldProcess, SupportsTransactions)

i.e. All of them can exist, but optional ones exist only if the cmdlet supports them. See Console Class for more information.

+11
source

Like this:

 function Get-CommonParameterNames { [CmdletBinding()] param() $MyInvocation.MyCommand.Parameters.Keys } 
+6
source

Source: https://habr.com/ru/post/986257/


All Articles