PowerShell提示特定输入,然后继续脚本。

huangapple go评论51阅读模式
英文:

PowerShell prompt for specific input then continue script

问题

我卡在这个PowerShell脚本上。

脚本执行以下操作:
当用户按下y时:1.bat打开并运行3秒,然后关闭。
然后脚本询问用户是否要再次运行1.bat,或者当他们按下q时脚本必须继续,但脚本结束。

#如果密码重置失败,询问是否重新开始
while($true) {
    $readHostValue = Read-Host -Prompt "如果密码重置失败,请输入y或q继续安装"
    
    switch ($readHostValue) {
        'y' { Start-Process C:\Users\deniz\Desktop.bat -Verb RunAs }
        'q' { return # 结束密码重置,继续脚本执行
        }
        Default { write-host "无效的输入,只能输入y或q!" }
    }
}

#脚本在这里停止,但需要继续执行以下内容:

#进度指示器/写入进度
Write-Progress -Activity "AfterLoader" -Status "Installing" -PercentComplete 10
Start-Sleep -Seconds 2
英文:

I'm stuck with this powershell script.

The script does the following:
When user presses y: 1.bat opens and runs for 3 second then closes.
Then the script asks the user if they want to run the 1.bat again, or when they press q the script NEEDS to continue, but the script ends

#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.bat -Verb RunAs }
    'q' { return # Ending password reset and continue script
        }
        Default { write-host "Invalid input y or n!" }
        }
    }

The script stops here, but needs to continue with the following:

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

答案1

得分: 2

Rav的回答指出,在switch语句的动作块中,return会退出包含它的 函数或脚本

相比之下,break 和 - 在某些情况下<sup>[1]</sup> - continue 只会退出 switch 语句,这意味着你的无限循环 (while ($true) { ... }) 会继续执行,除非你采取其他操作。

在这种情况下,你可以利用 break 语句的一种不常见的变体,即可以定位到一个 带标签的语句 来中断:

# 注意自选的 "menu:" 标签
:menu while ($true) {

  $readHostValue = Read-Host -Prompt 'If Password reset fails enter y or q to continue install'
  
  switch ($readHostValue) {
    'y'     { "running things..." }
    'q'     { break menu } # 退出 "menu:" 标记的 while 循环。
    default { Write-Host 'Invalid input y or q!' }
  }

}

'continuing...'

或者,你可以使用一个布尔变量来确定何时退出 while 循环:

$continue = $true
while ($continue) {

  $readHostValue = Read-Host -Prompt 'If Password reset fails enter y or q to continue install'
  
  switch ($readHostValue) {
    'y'     { "running things..." }
    'q'     { 
       $continue = $false # 信号表明 while 循环应该退出
       break              # 退出 switch 语句
    } 
    default { Write-Host 'Invalid input y or q!' }
  }

}

'continuing...'

<sup>[1] 对于 数组 值的 switch 输入,continue 继续到 下一个元素。如果输入是单个对象或单元素数组,continue 具有与 break 相同的效果,即退出语句。</sup>

英文:

<!-- language-all: sh -->

As Rav's answer points out, return in the context of a switch statement's action block exits the enclosing function or script.

By contrast, break and - situationally<sup>[1]</sup> - continue exit the switch statement only, which means that your indefinite loop (while ($true) { ... }) continues, unless you take additional action.

In this case you can take advantage of a rarely seen variant of the break statement, namely one that can target a labeled statement to break out of:

# Note the self-chosen &quot;menu:&quot; label
:menu while ($true) {

  $readHostValue = Read-Host -Prompt &#39;If Password reset fails enter y or q to continue install&#39;
  
  switch ($readHostValue) {
    &#39;y&#39;     { &quot;running things...&quot; }
    &#39;q&#39;     { break menu } # Break out of &quot;menu:&quot; labeled while loop.
    default { Write-Host &#39;Invalid input y or q!&#39; }
  }

}

&#39;continuing...&#39;

Alternatively, you could use a Boolean variable to determine when to exit the while loop:

$continue = $true
while ($continue) {

  $readHostValue = Read-Host -Prompt &#39;If Password reset fails enter y or q to continue install&#39;
  
  switch ($readHostValue) {
    &#39;y&#39;     { &quot;running things...&quot; }
    &#39;q&#39;     { 
       $continue = $false # signal that the while loop should exit
       break              # break out of the switch statement
    } 
    default { Write-Host &#39;Invalid input y or q!&#39; }
  }

}

&#39;continuing...&#39;

<sup>[1] With array-valued switch input, continue proceeds to the next element. If the input is a single object or a single-element array continue has the same effect as break, i.e. it exits the statement.</sup>

答案2

得分: 1

你的return语句退出整个脚本,而不仅仅是循环。这就是为什么你的脚本在那里停止,并且不执行其余的代码。

英文:

Your return statement exit the entire script, not just the loop. That’s why your script stops there and does not execute the rest of the code.

huangapple
  • 本文由 发表于 2023年5月11日 02:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定