gpt4 book ai didi

Powershell 开关中断标签未被执行

转载 作者:行者123 更新时间:2023-12-03 16:47:21 27 4
gpt4 key购买 nike

作为更大脚本的一部分,我已经实现了下面详述的 switch。目的是当脚本执行时,用户可以选择

  1. 在屏幕上输入要迁移的用户,或者
  2. 从文件导入。

如果选择了从文件导入选项——我想测试文件是否存在——如果没有,我想退出并返回开关标签 :choose。但是,当我选择从文件导入选项并提供一个不存在的路径时,脚本会继续并且不会中断或返回到标签。我哪里错了?

$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"

最佳答案

来自documentation for break :

In PowerShell, only loop keywords, such as Foreach, For, and While can have a label.

因此,尽管 switch 具有循环功能,但在这种情况下不被视为循环。

但是,在这种情况下,我不明白为什么没有标签的 break 是不够的。

关于Powershell 开关中断标签未被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com