2017-07-25 16 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:\> 

希望它能帮助。