2017-07-25 326 views
0

我對PowerShell相對較新。如何在PowerShell中提供用戶選擇方案?我可以通過讓用戶輸入參數來獲取參數,而不是讓他們從給定選項中進行選擇,從而編寫腳本。以下是我的方案使用powershell的輸入選擇

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): 

在指着我在正確的方向非常感謝任何幫助。謝謝你的時間

+2

使用PowerShell,最佳實踐方法是創建一個具有輸入參數的函數或腳本。但是,如果您想按照您展示的方式進行操作,可以使用「Read-Host」和「Write-Host」cmdlet。 – BenH

回答

1

這應該讓你開始。

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 
} 
+0

作爲一種最佳實踐,除非您確定不需要輸出,否則不應使用「寫入主機」。否則,你可以使用'Write-Output'或者'Write'(Write-Output的別名)或者甚至只在自己的行上引用來完成同樣的事情:輸出/成功(1)流上的字符串輸出。 – TheIncorrigible1

0

您可以使用

這種方式,你可以很容易地處理默認值從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:\> 

希望它能幫助。