Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

powershell - 在PowerShell中终止脚本(Terminating a script in PowerShell)

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

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

For example:

(例如:)

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

Of course there's no such thing as $host.Exit() .

(当然没有$host.Exit()这样的东西。)

There is $host.SetShouldExit() , but this actually closes the console window, which is not what I want.

(有$host.SetShouldExit() ,但这实际上关闭了控制台窗口,这不是我想要的。)

What I need is something equivalent to Python's sys.exit() that will simply stop execution of the current script without further adieu.

(我需要的是与Python的sys.exit()相当的东西,它将简单地停止执行当前脚本而不需要进一步的操作。)

Edit: Yeah, it's just exit .

(编辑:是的,它只是exit 。)

Duh.

(咄。)

  ask by kprobst translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I realize this is an old post but I find myself coming back to this thread a lot as it is one of the top search results when searching for this topic.

(我意识到这是一篇很老的帖子,但我发现自己回到这个帖子很多,因为它是搜索这个主题时的顶级搜索结果之一。)

However, I always leave more confused then when I came due to the conflicting information.

(但是,当我来到时,由于信息冲突,我总是更加困惑。)

Ultimately I always have to perform my own tests to figure it out.

(最终,我总是要进行自己的测试来弄明白。)

So this time I will post my findings.

(所以这次我会发布我的发现。)

TL;DR Most people will want to use Exit to terminate a running scripts.

(TL; DR大多数人都希望使用Exit来终止正在运行的脚本。)

However, if your script is merely declaring functions to later be used in a shell, then you will want to use Return in the definitions of said functions.

(但是,如果您的脚本只是声明稍后在shell中使用的函数,那么您将需要在所述函数的定义中使用Return 。)

Exit vs Return vs Break (退出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.

    (如果从shell调用此命令,它将退出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.

    (如果函数调用Exit命令,它将退出它正在运行的任何上下文。因此,如果仅从正在运行的脚本中调用该函数,它将退出该脚本。)

    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.

    (但是,如果您的脚本只是声明该函数以便可以从当前shell使用它并从shell运行该函数,它将退出shell,因为shell是运行Exit命令的函数正在运行的上下文。)

    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.

    (注意:默认情况下,如果右键单击脚本以在PowerShell中运行脚本,则一旦脚本运行完毕,PowerShell将自动关闭。)

    This has nothing to do with the Exit command or anything else in your script.

    (这与Exit命令或脚本中的任何其他内容无关。)

    It is just a default PowerShell behavior for scripts being ran using this specific method of running a script.

    (对于使用此特定运行脚本的方法运行的脚本,它只是一个默认的PowerShell行为。)

    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.

    (如果从脚本(任何函数外部)调用此命令,它将返回到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).

    (如果从shell调用此命令,它将返回到shell(这是从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 .

    (如果从shell调用脚本并且它包含任何函数外部的Return命令,那么当它返回到shell时,就没有更多命令可以运行,从而使得以这种方式使用的ReturnExit基本相同。)

  • 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.

    (如果你在一个嵌套在循环中的循环中调用Break ,它将只会突破它所调用的循环。)

    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.

    (Break还有一个有趣的特性,你可以在一个带有标签的循环前面,然后即使在该标记循环内的几个嵌套组中调用Break命令,你也可以打破标记循环。)

     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 } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...