Find Values ​​in ValidateSet

I was wondering if there is a way to get the values ​​used in the sentence Param()for ValidateSet. Something like this would be great:

Function Foo {
    Param (
        [ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
        [String]$Type = 'Startup'
    )

    $Type.ValidateSet
}

But, of course, there is Typeno such property in the object . Is it possible to get the values ​​set to ValidateSet?

+4
source share
3 answers
function Foo {
    param (
        [ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
        [String]$Type = 'Startup'
    )

    $ParameterList = (Get-Command -Name $MyInvocation.MyCommand).Parameters
    $ParameterList["Type"].Attributes.ValidValues
}

After your comment:

param (
        [ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
        [String]$Type = 'Startup'
)


(Get-Variable "Type").Attributes.ValidValues

The call Get-Variablealso works in function.

+5
source

All solutions below work both in functions and in scripts.

The most reliable solution that should work in any call scenario, PSv2 + :

param (
    [ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
    [String]$Type = 'Startup'
)

($MyInvocation.MyCommand.Parameters['Type'].Attributes |
  Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] }).ValidValues

A , PSv3 +, :

  • Set-StrictMode -version 1, .

    • Set-StrictMode , , , , PSv2 .
      ( Set-StrictMode : , ( ).

    • , module, Set-StrictMode .

  • , , Windows PowerShell v5.1/PowerShell Core v6.0-alpha16, script .

param (
    [ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
    [String]$Type = 'Startup'
)

(Get-Variable Type).Attributes.ValidValues

PSv3 + (Get-Variable Type).Attributes.ValidValues :

(Get-Variable Type).Attributes | ForEach-Object { $_.ValidValues }

.. PowerShell .Attributes .ValidValues .

.Attributes - [System.Management.Automation.ValidateSetAttribute] - .ValidValues, .

, , Set-StrictMode -version 2 , .

((Get-Variable Type).Attributes |
  Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] }).ValidValues

, ( -is ), , , .ValidValues.

[variable] $Type (Get-Variable Type).Attributes $MyInvocation.MyCommand.Parameters['Type'].Attributes.

$MyInvocation.MyCommand.Parameters , .


, ( ):

  • , .

  • Get-Command -Name $MyInvocation.MyCommand:

    • , $MyInvocation.MyCommand :
      $MyInvocation.MyCommand - [System.Management.Automation.ExternalScriptInfo] [System.Management.Automation.FunctionInfo] , [System.Management.Automation.CommandInfo], , Get-Commmand, , script/.

    • :

      • $MyInvocation.MyCommand - -Name, script script (, script.ps1) (, Foo).

      • script Get-Command script - script PATH ( , $env:PATH). , script, , , / PATH, , .
        : Get-Command -Name $MyInvocation.MyCommand , , script.

      • , :
        - PowerShell , , Foo , Get-Command -Name $MyInvocation.MyCommand Foo .
        ( Foo, Foo , , : & (Get-Item Function:Foo))

+3

validateScript, may provide a more flexible solution and will work well if you need additional parameter checking. It also allows you to get a list of valid parameters outside the function foowith the creation of the function get-validTypes.

Function Foo {
    Param (
        [validateScript({test-validTypes $_})]
        [String]$Type = 'Startup'
    )

    get-validTypes
}

function get-validTypes {

    $powerOptions = @('Startup', 'Shutdown', 'LogOn', 'LogOff')
    Write-Output $powerOptions

}

function test-validTypes {
[cmdletbinding()]
param ($typeInput)

    $validTypes = get-validTypes
    if ($validTypes.contains($typeInput)){
        return $true
    } else {
        Write-Error "Invalid Type Paramater, Must be on of the following: $powerOptions"
    }

}
-1
source

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


All Articles