我寫了一個小程式,但它有一個小問題。用戶應該輸入一個對應于數字的值。代碼的問題在于,無論何時您什么都不輸入,輸入一個不存在的值或關閉輸入視窗,它仍然會運行它后面的代碼。
$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#
我希望應用程式在 IX 退出輸入視窗時關閉并回傳到腳本的開頭,如果輸入了錯誤的值或根本沒有輸入,但我是 PowerShell 的新手,似乎無法弄清楚.
uj5u.com熱心網友回復:
正如Jeroen Mosterd評論的那樣,您可以使用具有相應名稱/值的哈希表而不是使用單獨的變數來簡化此操作。
此外,不要使用名為的變數,$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#
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478643.html
標籤:电源外壳
