Powershell 开关中断标签未被执行
Powershell Switch Break Label not being executed
作为更大脚本的一部分,我已经实现了下面详述的 switch
。目的是当脚本执行时,用户可以选择
- 在屏幕上输入要迁移的用户,或者
- 从文件导入。
如果从文件导入选项是 selected - 我想测试文件是否存在 - 如果不存在,我想退出并返回开关标签 :choose
。但是,当我 select 从文件选项导入并提供不存在的路径时,脚本会继续并且不会中断或 return 到标签。我哪里错了?
$chooseInputMethod = @"
This script migrates one or more user accounts between two trusted domains in a forest e.g. from domain1 to domain2 (or vice versa)
Select method to specify user(s) to migrate:
1. Enter name(s) on-screen (default)
2. Import name(s) from file
Enter selection number
"@
$choosePath = @"
Enter path to file..
Notes
- Filename:
If file is located in script directory ($pwd) you can enter the filename without a path e.g. users.txt
- No quotation marks:
DO NOT put any quotes around the path even if it contains spaces e.g. e:\temp\new folder\users.txt
Enter path or filename
"@
$enterUsernames = @"
Enter username(s) seperate each with a comma e.g. test1 or test1,test2,test3`n`nEnter name(s)
"@
cls
:choose switch (Read-Host $chooseInputMethod) {
1 { cls; $usersFromScreen = Read-Host $enterUsernames }
2 {
cls;
$usersFromFile = Read-Host $choosePath;
if (-not (Test-Path $usersFromFile -PathType Leaf)) {
break choose
}
}
default { cls; $usersFromScreen = Read-Host $enterUsernames }
}
Write-Host "hello"
In PowerShell, only loop keywords, such as Foreach, For, and While can have a label.
因此,switch
,即使它具有循环功能,在这种情况下也不被视为循环。
但是,在这种情况下,我不明白为什么 break
没有标签是不够的。
中断开关将退出它。只有一个循环(开关,适用于数组),因此普通中断和中断开关之间没有区别。您似乎希望另一个循环内的开关重复它。
# only runs once
:outer while (1) {
'outer'
while (1) {
'inner'
break outer
}
}
outer
inner
带有标签和循环的切换演示。文档并不完美。
# only runs once
:outer switch (1..10) {
default {
'outer'
foreach ($i in 1..10) {
'inner'
break outer
}
}
}
outer
inner
另一个开关演示。
switch (1..4) {
{ $_ % 2 } { "$_ is odd" }
default { "$_ is even" }
}
1 is odd
2 is even
3 is odd
4 is even
作为更大脚本的一部分,我已经实现了下面详述的 switch
。目的是当脚本执行时,用户可以选择
- 在屏幕上输入要迁移的用户,或者
- 从文件导入。
如果从文件导入选项是 selected - 我想测试文件是否存在 - 如果不存在,我想退出并返回开关标签 :choose
。但是,当我 select 从文件选项导入并提供不存在的路径时,脚本会继续并且不会中断或 return 到标签。我哪里错了?
$chooseInputMethod = @"
This script migrates one or more user accounts between two trusted domains in a forest e.g. from domain1 to domain2 (or vice versa)
Select method to specify user(s) to migrate:
1. Enter name(s) on-screen (default)
2. Import name(s) from file
Enter selection number
"@
$choosePath = @"
Enter path to file..
Notes
- Filename:
If file is located in script directory ($pwd) you can enter the filename without a path e.g. users.txt
- No quotation marks:
DO NOT put any quotes around the path even if it contains spaces e.g. e:\temp\new folder\users.txt
Enter path or filename
"@
$enterUsernames = @"
Enter username(s) seperate each with a comma e.g. test1 or test1,test2,test3`n`nEnter name(s)
"@
cls
:choose switch (Read-Host $chooseInputMethod) {
1 { cls; $usersFromScreen = Read-Host $enterUsernames }
2 {
cls;
$usersFromFile = Read-Host $choosePath;
if (-not (Test-Path $usersFromFile -PathType Leaf)) {
break choose
}
}
default { cls; $usersFromScreen = Read-Host $enterUsernames }
}
Write-Host "hello"
In PowerShell, only loop keywords, such as Foreach, For, and While can have a label.
因此,switch
,即使它具有循环功能,在这种情况下也不被视为循环。
但是,在这种情况下,我不明白为什么 break
没有标签是不够的。
中断开关将退出它。只有一个循环(开关,适用于数组),因此普通中断和中断开关之间没有区别。您似乎希望另一个循环内的开关重复它。
# only runs once
:outer while (1) {
'outer'
while (1) {
'inner'
break outer
}
}
outer
inner
带有标签和循环的切换演示。文档并不完美。
# only runs once
:outer switch (1..10) {
default {
'outer'
foreach ($i in 1..10) {
'inner'
break outer
}
}
}
outer
inner
另一个开关演示。
switch (1..4) {
{ $_ % 2 } { "$_ is odd" }
default { "$_ is even" }
}
1 is odd
2 is even
3 is odd
4 is even