在 PowerShell 中终止脚本 [英] Terminating a script in PowerShell

查看:51
本文介绍了在 PowerShell 中终止脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been looking for a way to terminate a PowerShell (PS1) script when an unrecoverable error occurs within a function. For example:

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

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

Of course there's no such thing as $host.Exit(). There is $host.SetShouldExit(), but this actually closes the console window, which is not what I want. What I need is something equivalent to Python's sys.exit() that will simply stop execution of the current script without further adieu.

是的,它只是exit.呃.

推荐答案

你应该使用 exit 关键字.

You should use the exit keyword.

这篇关于在 PowerShell 中终止脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆