1

I am relatively new to powershell .How can i provide a user selection scenario in powershell ?. I am able to write the script by getting the parameters from the user by having them type it instead of just letting them select from the given options . The below is my scenario

Env:
  1) staging
  2) prod 
Selection:

Select action to perform:
  1) foo
  2) bar
Selection:

Schedule or leave blank to schedule now (yyyy-mm-dd hh:mm:ss):

note (leave blank to skip):

Plan of action:
  >> Sending action to:
  >> Scheduling a action of:
  >> Schedule date: 
  >> Notes:
Ok to proceed? (Y|N):

Any help in pointing me in the right direction is much appreciated. Thanks for your time

1
  • 2
    With PowerShell the best practice method would be to make a function or script that has parameters for the input. But if you wanted to do the way you showed, you could use Read-Host and Write-Host cmdlets. Commented Jul 25, 2017 at 17:26

2 Answers 2

3

You can use

This way you can handle easily default value for choice from a list, ensure that the date entered is a valid DateTime and provide user friendly (because PowerShell standard) help for the requested inputs.

See the snippet below:

$envChoice = [System.Management.Automation.Host.ChoiceDescription[]](@(
    (New-Object System.Management.Automation.Host.ChoiceDescription("&Staging", "Staging environment")),
    (New-Object System.Management.Automation.Host.ChoiceDescription("&Prod", "Production environment"))
))
$actionChoice = [System.Management.Automation.Host.ChoiceDescription[]](@(
    (New-Object System.Management.Automation.Host.ChoiceDescription("&Foo", "foo")),
    (New-Object System.Management.Automation.Host.ChoiceDescription("&Bar", "bar"))
))
$environment = $Host.Ui.PromptForChoice("Environment", "Choose the target environment", $envChoice, 0)
$action = $Host.Ui.PromptForChoice("Action", "Select action to perform", $actionChoice, 0)

$dateFormat = "yyyy-MM-dd HH:mm:ss"
$schedule = [String]::Empty
$schedTime = [DateTime]::MinValue
[System.Globalization.CultureInfo]$provider = [System.Globalization.CultureInfo]::InvariantCulture
while (-not [DateTime]::TryParseExact($schedule, $dateFormat, $provider, [System.Globalization.DateTimeStyles]::None, [ref]$schedTime)) {
    Write-Output ("Schedule or leave blank to schedule now ({0}):" -f $dateFormat.ToLower())
    $schedule = Read-Host
    if ([String]::IsNullOrEmpty($schedule)) {
        $schedule = [DateTime]::Now.ToString($dateFormat)
    }
}

Write-Output "Note (leave blank to skip):"
$note = Read-Host

$confirmChoice = [System.Management.Automation.Host.ChoiceDescription[]](@(
    (New-Object System.Management.Automation.Host.ChoiceDescription("&Yes","Confirm")),
    (New-Object System.Management.Automation.Host.ChoiceDescription("&No","Cancel"))
))
$answer = $Host.Ui.PromptForChoice((@"
Plan of action:
  >> Sending action to: {0}
  >> Scheduling a action of: {1}
  >> Schedule date: {2:yyyy-MM-dd HH:mm:ss}
  >> Notes: {3}
"@ -f $envChoice[$environment].Label.Replace("&",""),$actionChoice[$action].Label.Replace("&",""),$schedTime,$note),"Ok to proceed?",$confirmChoice,0)

Switch ($answer){
    0 {"Should proceed"; break}
    1 {"Cancelled"; break}
}

Output looks like this:

PS C:\> 45309660.ps1

Environment
Choose the target environment
[S] Staging  [P] Prod  [?] Help (default is "S"): ?
S - Staging environment
P - Production environment
[S] Staging  [P] Prod  [?] Help (default is "S"): P

Action
Select action to perform
[F] Foo  [B] Bar  [?] Help (default is "F"): ?
F - foo
B - bar
[F] Foo  [B] Bar  [?] Help (default is "F"): B
Schedule or leave blank to schedule now (yyyy-mm-dd hh:mm:ss):
2017-08-04 17:04:54
Note (leave blank to skip):
This is not a note

Plan of action:
  >> Sending action to: Prod
  >> Scheduling a action of: Bar
  >> Schedule date: 2017-08-04 17:04:54
  >> Notes: This is not a note
Ok to proceed?
[Y] Yes  [N] No  [?] Help (default is "Y"): N
Cancelled
PS C:\> 45309660.ps1

Environment
Choose the target environment
[S] Staging  [P] Prod  [?] Help (default is "S"):

Action
Select action to perform
[F] Foo  [B] Bar  [?] Help (default is "F"):
Schedule or leave blank to schedule now (yyyy-mm-dd hh:mm:ss):

Note (leave blank to skip):


Plan of action:
  >> Sending action to: Staging
  >> Scheduling a action of: Foo
  >> Schedule date: 2017-08-03 13:08:00
  >> Notes:
Ok to proceed?
[Y] Yes  [N] No  [?] Help (default is "Y"):
Should proceed
PS C:\>

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

This should get you started.

Function Do-Stuff
{
    Param($Environment,$Action,$Schedule,$Note)

    <# logic #>
}
$Splat = @{
    Environment='';
    Action='';
    Schedule='';
    Note='';
}

Write-Host "Env:`r`n`t1) staging`r`n`t2) prod`r`nSelection:"
$Splat.Environment = Read-Host

Write-Host "Select action to perform:`r`n`t1) foo`r`n`t2) bar`r`nSelection:"
$Splat.Action = Read-Host

Write-Host "Schedule or leave blank to schedule now (yyyy-mm-dd hh:mm:ss):"
$Splat.Schedule = Read-Host

Write-Host "note (leave blank to skip):"
$Splat.Note = Read-Host

Write-Host @"
Plan of action:
  >> Sending action to: $($Splat.Environment)
  >> Scheduling a action of: $($Splat.Action)
  >> Schedule date: $($Splat.Schedule)
  >> Notes: $($Splat.Note)
Ok to proceed? (Y|N):"@
$Agree = Read-Host
If ($Agree.ToUpper() -eq 'Y')
{
    Do-Stuff @Splat
}

1 Comment

As a best practice, you shouldn't use Write-Host unless you're certain you don't need the output. Otherwise you can use Write-Output or Write (alias for Write-Output) or even just quoting on its own line will accomplish the same thing: string output on the output/success (1) stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.