如何在 Windows PowerShell 中終止腳本

  1. 使用 Exit 命令來終止 Windows PowerShell 中的腳本
  2. 使用 Throw 命令來終止 Windows PowerShell 中的腳本
  3. Return 命令
  4. break 命令
  5. continue 命令
如何在 Windows PowerShell 中終止腳本

在 Windows PowerShell 中有許多終止腳本的方法。然而,雖然它們在上下文上聽起來相似,但在功能上,它們的實際用途彼此不同。

本文將列舉在 Windows PowerShell 中終止一個腳本的方法,並逐一定義它們。

使用 Exit 命令來終止 Windows PowerShell 中的腳本

Exit 命令將根據其名稱退出您的腳本。如果您沒有打開的會話,則此命令還會關閉您的 shell 或腳本窗口。Exit 命令還有助於通過使用退出碼提供反饋。

exit

僅運行 exit 命令可以有一個退出碼 0(默認),表示成功或正常終止,或 1,表示失敗或未捕獲的異常。

退出碼的優點在於退出碼是完全可自定義的。只要退出碼是整數,則該退出碼是有效的。此外,要知道最後的退出碼,您可以輸出變量 $LASTEXITCODE

Exit.ps1

Write-Output 'Running sample script.'
exit 200

示例代碼:

PS C:\>powershell.exe .\Exit.ps1
PS C:\>Running sample script.
PS C:\>Write-Output $LASTEXITCODE
PS C:\>200

還要記住,當您從運行的 PowerShell 腳本中調用另一個 PowerShell 文件並使用 exit 命令時,運行的腳本也將終止。

使用 Throw 命令來終止 Windows PowerShell 中的腳本

Throw 命令與 Exit 命令類似,帶有退出碼,但更具信息量。您可以使用該命令和自定義表達式來生成終止錯誤。通常,Throw 命令在 Try-Catch 表達式的 Catch 區塊內部使用,以適當描述異常。

示例代碼:

Try {
    $divideAnswer = 1 / 0
}
Catch {
    Throw "The mathematical expression has a syntax error"
}

輸出:

The mathematical expression has a syntax error
At line:4 char:5
+     Throw "The mathematical expression has a syntax error"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (The mathematica... a syntax error:String) [], RuntimeException
    + FullyQualifiedErrorId : The mathematical expression has a syntax error

Return 命令

Exit 命令不同,Return 命令將返回到其之前的調用點,而不會關閉您的腳本窗口。

通常,我們使用 Return 命令從腳本中的某個執行函數返回值。

示例代碼:

Function sumValues($int1, $int2) {
    Return ($int1 + $int2)
}

# The function sumValues is called below, and the script will return to 
# the same line with a value and store it in the output variable
$output = sumValues 1 2 

Write-Output $output

輸出:

3

break 命令

我們使用 break 命令來終止循環和情況。

示例代碼:

$numList = 1, 2, 3, 4, 5, 6, 7, 8

foreach ($number in $numList) {
    if ($number -eq 8) {
        #Terminate loop if number variable is equal to 8
        break
    }

    Write-Output $number
}

輸出:

1
2
3
4
5
6
7

如果我們有嵌套循環,我們只會從調用 break 命令的循環中跳出。

示例代碼:

While ($true) {
    While ($true) {
        #The break command will only break out of this while loop
        break
    }
    #The script will continue to run on this line after breaking out of the inner while loop
}

如果您想要跳出特定的嵌套循環,break 命令將標籤作為其參數。

While ($true) {
    :thisLoop While ($true) {
        While ($true) {
            #The break command below will break out of the `thisLoop` while loop.
            Break thisLoop
        }
    }
}

continue 命令

continue 命令也在循環級別終止腳本。不過,continue 命令不會立即終止整個循環,而是僅終止當前的迭代,並繼續循環,直到所有迭代都被處理過。

我們可以說這是一個在執行循環時跳過某些內容的命令。

示例代碼:

$numList = 1, 2, 3, 4, 5, 6, 7, 8

foreach ($number in $numList) {
    if ($number -eq 2) {
        #The continue command below will skip number 2 and will directly process the next iteration, resetting the process to the top of the loop.
        continue
    }

    Write-Output $number
}

輸出:

1
3
4
5
6
7
8
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

相關文章 - PowerShell Script