1

I am using the below code in PoweerShell script:-

$options = @{
    "A"="a"
    "B"="b"
}

$props = @{
    Parameters = @(
        @{Name="selectedOption"; Title="Choose an option"; Options=$options; Tooltip="Choose one."}
    )
    Title = "Option selector"
    Description = "Choose the right option."
    Width = 300
    Height = 300
    ShowHints = $true
}

Read-Variable @props

I need to get selected dropdown values. However, it is showing ok or cancel result when I click on ok and cancel button in dialog box. I have tried $props.Parameters.Value as well it is showing the dropdown values but not the selected value everytime.

2 Answers 2

3

You should check $selectedOption value after you execute the script.

That's were the value of what user selected is stored.

Your parameters contain:

{Name="selectedOption"; Title="Choose an option"; Options=$options; Tooltip="Choose one."}

Look at the part Name="selectedOption". That means that your value will be stored in $selectedOption variable.

And you should only process if $result returns ok, like that:

$options = @{ "A"="a" "B"="b" }

$props = @{
    Parameters = @(
        @{Name="selectedOption"; Title="Choose an option"; Options=$options; Tooltip="Choose one."}
    )
    Title = "Option selector"
    Description = "Choose the right option."
    Width = 300
    Height = 300
    ShowHints = $true
}

$result = Read-Variable @props

if($result -eq "ok") 
{
    $selectedOption
}
1

You need to update your code to get value like this.

$options = @{
"A"="a"
"B"="b"
}

$props = @{
Parameters = @(
    @{Name="selectedOption"; Title="Choose an option"; Options=$options; Tooltip="Choose one."}
)
Title = "Option selector"
Description = "Choose the right option."
Width = 300
Height = 300
ShowHints = $true
}

Read-Variable @props
$selectedOption

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.