Display PowerShell mandatory parameter options?

Welcome Forums General PowerShell Q&A Display PowerShell mandatory parameter options?

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
6 months, 4 weeks ago.

  • Author
    Posts
  • #99121

    Participant
    Points: 0
    Rank: Member

    Hi everyone,

    I tried posting this over at Stack Overflow but didn't get a great answer. Apparently this community is better for PowerShell though so I hope someone can help :).

    Is there a way to have PowerShell display the options possible for a parameter? I have the following Parameter which requires one of the resource groups in Azure to be selected.

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Enter the name of the resource group you would like to use.")]
        [ValidateScript( {$_ -in (Get-AzureRMResourceGroup | Select-Object -ExpandProperty ResourceGroupName)})]
        [String]$ResourceGroup
    )

    ValidateScript will check to see if it is one of the Resource groups in Azure, but my question is how can I display a list of the resource groups so that the person running the script knows what possible options they can input for the parameter? Can I use Write-Host or something within the Param block?

    If possible I don't want to maintain the options manually so if it could check the resource groups in Azure and then provide a list for the script runner to choose from that would be great.

    thanks

  • #99123

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    ValidateScript is an odd puppy in terms of tab completion. Using a normal parameter, no, PowerShell can't auto-complete. What you're after is a Dynamic Parameter, I suspect, which let you run code in different places to accomplish that. Dig into https://foxdeploy.com/2017/01/13/adding-tab-completion-to-your-powershell-functions/ for some examples.

    That said, tab completion is what PowerShell offers – there's no pop-up menus, or on-screen lists, or anything else. So if you want something more than tab completion of available values, you're entirely on your own.

  • #99124

    Participant
    Points: 0
    Rank: Member

    Hi everyone,

    I recently asked this question over at Stack Overflow but didn't get a great answer, I hope someone here can help.

    Is there a way to have PowerShell display the options possible for a parameter? I have the following Parameter which requires one of the resource groups in Azure to be selected.

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Enter the name of the resource group you would like to use.")]
        [ValidateScript( {$_ -in (Get-AzureRMResourceGroup | Select-Object -ExpandProperty ResourceGroupName)})]
        [String]$ResourceGroup
    )

    ValidateScript will check to see if it is one of the Resource groups in Azure, but my question is how can I display a list of the resource groups so that the person running the script knows what possible options they can input for the parameter? Can I use Write-Host or something within the Param block?

    I would prefer it was a dynamic list as each person that runs the script may have different resource group options to choose.

    thanks

The topic ‘Display PowerShell mandatory parameter options?’ is closed to new replies.