您可以使用
這種方式,你可以很容易地處理默認值從lis t,確保輸入的日期是有效的DateTime
,並且爲請求的輸入提供用戶友好的(因爲PowerShell標準)幫助。
請參見下面的代碼片段:
$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}
}
輸出看起來是這樣的:
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:\>
希望它能幫助。
使用PowerShell,最佳實踐方法是創建一個具有輸入參數的函數或腳本。但是,如果您想按照您展示的方式進行操作,可以使用「Read-Host」和「Write-Host」cmdlet。 – BenH