You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本故障:输入q后无法继续执行后续代码求助

问题解决:PowerShell输入q后脚本终止的问题

问题原因

你的脚本中,switch分支里的return语句在顶级脚本作用域执行时,会直接终止整个脚本,而非仅退出while循环,导致后续进度指示代码无法运行。

修复方案

将return替换为break,break会跳出当前的while循环,脚本即可继续执行后续内容。同时修正默认分支里的输入提示错误(原提示写的是y或n,实际应为y或q)。

修改后的完整脚本

#Asks to start Password reset again if it fails
while($true) {
    $readHostValue = Read-Host -Prompt "If Password reset fails enter y or q to continue install"

    switch ($readHostValue) {
        'y' { Start-Process C:\Users\deniz\Desktop\1.bat -Verb RunAs }
        'q' { break # 退出循环,继续执行后续脚本 }
        Default { Write-Host "Invalid input! Please enter y or q!" }
    }
}

#progress indicators/write-progress
Write-Progress -Activity "AfterLoader" -Status "Installing" -PercentComplete 10
Start-Sleep -Seconds 2

补充说明

  • break在switch中使用时,会跳出当前switch块,同时终止外层的while($true)循环,脚本流程自然进入后续代码段。
  • 若需要等待批处理文件运行完毕后再回到循环提问,可给Start-Process添加-Wait参数:Start-Process C:\Users\deniz\Desktop\1.bat -Verb RunAs -Wait。

内容的提问来源于stack exchange,提问作者Deniz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 14:17:27