有没有办法暂时返回命令提示符

S. *_*sen 5 powershell user-interaction command-line-interface

如何允许用户“暂停当前管道并返回到命令提示符”,然后在 powershell 脚本中恢复?

我在一篇有关 Powershell 中的用户交互的博客文章中偶然发现了这一行

$suspend = New-Object System.Management.Automation.Host.ChoiceDescription "&Suspend", "Pause the current pipeline and return to the command prompt. Type ""exit"" to resume the pipeline."
Run Code Online (Sandbox Code Playgroud)

这是提示中的一个模拟选项,模仿本机命令(Remove-Item)的外观。你瞧:该命令实际上实现了该行为。进行了快速的 Google 搜索,我没有在脚本中找到实现。

Mat*_*sen 6

您可以使用$Host.EnterNestedPrompt()暂停当前操作来输入“嵌套”提示符 - 一旦退出,执行就会恢复(使用 或exit$Host.ExitNestedPrompt()

function f {
  param([switch]$Intervene)

  $abc = 123

  if($Intervene.IsPresent){
    $host.EnterNestedPrompt()
  }

  Write-Host "`$abc has value '$abc'"
}
Run Code Online (Sandbox Code Playgroud)

现在尝试使用和不使用-Intervene开关调用该函数:

在此输入图像描述