Terminating a script in PowerShell

前端 未结 10 1875
走了就别回头了
走了就别回头了 2020-11-28 01:02

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() {
    #         


        
10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 01:12

    I coincidentally found out that Break (e.g. simply Break Script, where the label Script doesn't exists) appears to break out of the entire script (even from within a function) and keeps the host alive.
    This way you could create a function that breaks the script from anywhere (e.g. a recursive loop) without knowing the current scope (and creating labels):

    Function Quit($Text) {
        Write-Host "Quiting because: " $Text
        Break Script
    } 
    

提交回复
热议问题