PowerShell - 根据用户输入采取行动?
PowerShell - Taking Action Based Off of User Input?
我想通过 PromptForChoice 请求用户输入,然后根据他们select 执行特定操作。
在我的代码中,我有三个选项:笔记本电脑、平板电脑和工作站。如果用户 select 是一个,我想执行某个操作,而不是分配给其他选项的其他操作。
在代码中,我不太理解包含决策变量的行,或者 $choices 之后数字 1 的含义。我在我不理解的部分代码周围加了星号。
我不知道如何将某些操作分配给一个按钮。代码的第一部分是我想做但不起作用的部分,最后一部分是实际的脚本。
$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'
}
尽管如此,在 ISE 或控制台主机中;您不会从此代码中取回字符串。你得到一个 array/index 号码。
$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
#>
因此,您需要询问该号码,然后使用条件逻辑分支到您想要执行的任何其他操作。多个或嵌套 if/then/else、try/catch。或 switch 语句。
$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
#>
我想通过 PromptForChoice 请求用户输入,然后根据他们select 执行特定操作。
在我的代码中,我有三个选项:笔记本电脑、平板电脑和工作站。如果用户 select 是一个,我想执行某个操作,而不是分配给其他选项的其他操作。
在代码中,我不太理解包含决策变量的行,或者 $choices 之后数字 1 的含义。我在我不理解的部分代码周围加了星号。
我不知道如何将某些操作分配给一个按钮。代码的第一部分是我想做但不起作用的部分,最后一部分是实际的脚本。
$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'
}
尽管如此,在 ISE 或控制台主机中;您不会从此代码中取回字符串。你得到一个 array/index 号码。
$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
#>
因此,您需要询问该号码,然后使用条件逻辑分支到您想要执行的任何其他操作。多个或嵌套 if/then/else、try/catch。或 switch 语句。
$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
#>