Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用按键停止powershell_Powershell_Administrator - Fatal编程技术网

用按键停止powershell

用按键停止powershell,powershell,administrator,Powershell,Administrator,如何通过按键停止Powershell。我有一个循环,当我按下q时,它应该停止。如何做到这一点 我找到了以下代码,但不起作用 while($true) { Write-Host "Working" -BackgroundColor Green if($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyup,NoEcho").Character)) { Write-H

如何通过按键停止Powershell。我有一个循环,当我按下q时,它应该停止。如何做到这一点

我找到了以下代码,但不起作用

while($true) {
    Write-Host "Working" -BackgroundColor Green

    if($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyup,NoEcho").Character)) {
        Write-Host "Exiting now, don't try to stop me...." -BackgroundColor DarkRed
        break;
    }
}

有人有主意吗?

如果您想退出程序本身,可以使用Ctrl+C,它将停止脚本。
您可以更具体地说,如果您有一些代码要在循环后运行,在这种情况下,这将无法工作。

相同的代码可以工作,但不是在PowerShell ISE中,而是在普通的PowerShell控制台中。如果要编写脚本,你们必须保存它,然后通过Powershell控制台执行。

我将在脚本上附加一个GUI。这就是为什么我需要指定某个可以与GUI中的按钮连接的东西,如果按下该按钮,则整个程序链接。应该停止GUI和脚本。而是将其与变量连接并使用while循环