我一直在寻找一种方法,在函数中发生不可恢复的错误时终止PowerShell (PS1)脚本。例如:

function foo() {
    # Do stuff that causes an error
    $host.Exit()
}

当然,没有$host.Exit()这样的东西。有$host.SetShouldExit(),但这实际上关闭了控制台窗口,这不是我想要的。我需要的是一些相当于Python的sys.exit(),它将简单地停止当前脚本的执行,而不需要进一步的告别。

编辑:是的,只是退出。咄。


当前回答

我知道这是一个老帖子,但我发现自己经常回到这个帖子,因为它是搜索这个主题时的头号搜索结果之一。然而,我总是离开比我来的时候更困惑,因为相互矛盾的信息。最终,我总是不得不执行自己的测试来弄清楚。所以这次我将把我的发现发布出来。

大多数人都希望使用Exit来终止正在运行的脚本。然而,如果你的脚本只是声明了稍后在shell中使用的函数,那么你就需要在函数的定义中使用Return。

退出vs返回vs打破

Exit: This will "exit" the currently running context. If you call this command from a script it will exit the script. If you call this command from the shell it will exit the shell. If a function calls the Exit command it will exit what ever context it is running in. So if that function is only called from within a running script it will exit that script. However, if your script merely declares the function so that it can be used from the current shell and you run that function from the shell, it will exit the shell because the shell is the context in which the function contianing the Exit command is running. Note: By default if you right click on a script to run it in PowerShell, once the script is done running, PowerShell will close automatically. This has nothing to do with the Exit command or anything else in your script. It is just a default PowerShell behavior for scripts being ran using this specific method of running a script. The same is true for batch files and the Command Line window. Return: This will return to the previous call point. If you call this command from a script (outside any functions) it will return to the shell. If you call this command from the shell it will return to the shell (which is the previous call point for a single command ran from the shell). If you call this command from a function it will return to where ever the function was called from. Execution of any commands after the call point that it is returned to will continue from that point. If a script is called from the shell and it contains the Return command outside any functions then when it returns to the shell there are no more commands to run thus making a Return used in this way essentially the same as Exit. Break: This will break out of loops and switch cases. If you call this command while not in a loop or switch case it will break out of the script. If you call Break inside a loop that is nested inside a loop it will only break out of the loop it was called in. There is also an interesting feature of Break where you can prefix a loop with a label and then you can break out of that labeled loop even if the Break command is called within several nested groups within that labeled loop. While ($true) { # Code here will run :myLabel While ($true) { # Code here will run While ($true) { # Code here will run While ($true) { # Code here will run Break myLabel # Code here will not run } # Code here will not run } # Code here will not run } # Code here will run }

其他回答

退出也会退出PowerShell。如果你想“打破”当前的函数或脚本-使用break:)

If ($Breakout -eq $true)
{
     Write-Host "Break Out!"
     Break
}
ElseIf ($Breakout -eq $false)
{
     Write-Host "No Breakout for you!"
}
Else
{
    Write-Host "Breakout wasn't defined..."
}

也许用“陷阱”更好。PowerShell trap指定当发生终止或错误时要运行的代码块。类型

Get-Help about_trap

了解更多关于trap语句的信息。

我用它来重播一个节目。我不知道它是否有帮助,但它是一个简单的if语句,只需要两个不同的条目。它在powershell中为我工作。

$rerun = Read-Host "Rerun report (y/n)?"

if($rerun -eq "y") { Show-MemoryReport }
if($rerun -eq "n") { Exit }

不知道这是否有帮助,但我相信这将沿线终止程序后,你已经运行它。但是,在这种情况下,每个定义的输入都需要一个列出并分类的输出。您还可以让退出调用显示一个新的提示行,并以这种方式终止程序。

我认为你在寻找回归而不是打破。Break通常用于循环,并且只从最内部的代码块中中断。使用Return命令退出函数或脚本。

您应该使用exit关键字。