I want to request user input via PromptForChoice and then, based on what they select, do a certain action.
In my code I have three options: Laptop, Tablet, and Workstation . If the user selects one, I want to do a certain action as opposed to the other actions assigned to the other options.
In the code, I do not really understand the line containing the decision variable, or what the number 1 means either after $choices. I have put asterisks around the parts of the code I don't understand.
I do not know how to assign certain actions to one button. The first part of code is what I want to do but isn't working, and the last part is the actual script.
$decision = $Host.UI.PromptForChoice($title, $question, $choices, **1**)
if ($decision -eq **Laptop**)
$title = 'PC Type Selection'
$question = 'Select Object Type'
$choices = '&Laptop','&Tablet' ,'&Workstation'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'confirmed'
} else {
Write-Host 'cancelled'
}
In the ISE or the console host, notwithstanding; you don't get back a string from this code. You get an array/index number.
$title = 'PC Type Selection'
$question = 'Select Object Type'
$choices = '&Laptop','&Tablet' ,'&Workstation'
<#
Using PowerShell's variable squeezing to assign the results and
output to screen at the same time.
This, not a requirement, just a demo of how to see your variable
content on use.
#>
($decision = $Host.UI.PromptForChoice($title, $question, $choices, 1))
# Results
<#
0
#>
# Results
<#
1
#>
# Results
<#
2
#>
So, you need to ask for that number, then branch to whatever else you'd want to do, using conditional logic. Multiple or nested if/then/else, try/catch. or switch statement.
$title = 'PC Type Selection'
$question = 'Select Object Type'
$choices = '&Laptop','&Tablet' ,'&Workstation'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
switch ($decision)
{
0 {'You selected Laptop'}
1 {'You selected Tablet'}
2 {'You selected Workstation'}
Default {}
}
# Results
<#
You selected Laptop
#>
# Results
<#
You selected Tablet
#>
# Results
<#
You selected Workstation
#>