输入 window 关闭时关闭应用程序,如果输入为空或不对应值,则 return 启动
Close application when input window is closed and return to start if input is empty or doesn't correspond to value
我写了一个小程序,但是有一个小问题。用户应该输入一个对应于数字的值。代码的问题在于,无论何时您什么都不输入,输入不存在的值或关闭输入 window,它仍然会运行后面的代码。
$A = 87
$B = 130
$C = 80
$D = 83
$E = 78
$F = 92
$input = $(
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::InputBox('Select a computer','Test', 'row/column')
)
if($input -eq 'E2')
{
Set-Variable -Name "ip" -Value $A
}
if($input -eq 'A2')
{
Set-Variable -Name "ip" -Value $B
}
if($input -eq 'D3')
{
Set-Variable -Name "ip" -Value $C
}
if($input -eq 'C3')
{
Set-Variable -Name "ip" -Value $D
}
if($input -eq 'E4')
{
Set-Variable -Name "ip" -Value $E
}
if($input -eq 'F4')
{
Set-Variable -Name "ip" -Value $F
}
#remaining code#
我希望应用程序在输入 window & return 到脚本开头时关闭,如果输入了错误的值或 none ,但我是 PowerShell 的新手,似乎无法理解。
正如 评论的那样,使用具有相应 Name/Values 的哈希表而不是使用单独的变量可以使这更容易。
此外,不要使用名为 $input
的变量,因为它在 PowerShell
中是一个 Automatic variable
尝试这样的事情:
$hash = @{
E2 = 87
A2 = 130
D3 = 80
C3 = 83
E4 = 78
F4 = 92
}
Add-Type -AssemblyName Microsoft.VisualBasic
do {
$ip = $null
$choice = [Microsoft.VisualBasic.Interaction]::InputBox('Type the name of a computer','Test')
# exit the loop if the user cancels the box or clicks OK with an emty value
if ([string]::IsNullOrWhiteSpace($choice)) { break }
$ip = $hash[$choice]
} until ($ip)
if (!$ip) { exit }
# remaining code#
我写了一个小程序,但是有一个小问题。用户应该输入一个对应于数字的值。代码的问题在于,无论何时您什么都不输入,输入不存在的值或关闭输入 window,它仍然会运行后面的代码。
$A = 87
$B = 130
$C = 80
$D = 83
$E = 78
$F = 92
$input = $(
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::InputBox('Select a computer','Test', 'row/column')
)
if($input -eq 'E2')
{
Set-Variable -Name "ip" -Value $A
}
if($input -eq 'A2')
{
Set-Variable -Name "ip" -Value $B
}
if($input -eq 'D3')
{
Set-Variable -Name "ip" -Value $C
}
if($input -eq 'C3')
{
Set-Variable -Name "ip" -Value $D
}
if($input -eq 'E4')
{
Set-Variable -Name "ip" -Value $E
}
if($input -eq 'F4')
{
Set-Variable -Name "ip" -Value $F
}
#remaining code#
我希望应用程序在输入 window & return 到脚本开头时关闭,如果输入了错误的值或 none ,但我是 PowerShell 的新手,似乎无法理解。
正如
此外,不要使用名为 $input
的变量,因为它在 PowerShell
尝试这样的事情:
$hash = @{
E2 = 87
A2 = 130
D3 = 80
C3 = 83
E4 = 78
F4 = 92
}
Add-Type -AssemblyName Microsoft.VisualBasic
do {
$ip = $null
$choice = [Microsoft.VisualBasic.Interaction]::InputBox('Type the name of a computer','Test')
# exit the loop if the user cancels the box or clicks OK with an emty value
if ([string]::IsNullOrWhiteSpace($choice)) { break }
$ip = $hash[$choice]
} until ($ip)
if (!$ip) { exit }
# remaining code#