如何在 PowerShell 中確認提示

  1. 在 PowerShell 中使用 -Confirm 開關以進行確認提示
  2. 在 PowerShell 中使用 if 語句進行確認提示
  3. 在 PowerShell 中使用 PromptForChoice() 方法進行確認提示
  4. 在 PowerShell 中使用基於函數的方法進行確認提示
  5. 結論
如何在 PowerShell 中確認提示

確認提示的基本概念是創建一個選項,以便在不同的操作之間切換。例如,當用戶選擇時,腳本應繼續運行,而當用戶選擇時,腳本應退出。

PowerShell 可以在執行任何操作之前自動向用戶請求確認,使用 -Confirm 開關。本教程將介紹不同的方法來請求用戶確認以便在 PowerShell 中繼續任何操作。

在 PowerShell 中使用 -Confirm 開關以進行確認提示

PowerShell 擁有一組影響其操作環境的偏好變數,並允許您自定義其行為。$ConfirmPreference 是其中一個偏好變數,用於決定 PowerShell 在執行 cmdlet 或函數之前是否自動提示您確認。

它幫助在執行風險低、中或高的 cmdlet 或函數之前提示確認。由於大多數 cmdlet 和函數的風險為中等,因此如果變數的值設置為 High,它不會請求確認。

$ConfirmPreference

輸出:

High

您必須在命令中使用 -Confirm 開關以提示用戶確認。這會迫使命令在 PowerShell 中顯示確認提示。

更簡單的使用 -Confirm 開關的方法是通過在 Remove-Item cmdlet 中使用它,這可以在更簡單的命令中產生確認操作。以下命令顯示了其用法的示例。

命令:

Remove-Item test.txt -Confirm

輸出:

Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Users\rhntm\test.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): N

在輸出中,-Confirm 開關迫使 PowerShell 顯示確認提示。這種方法直觀且適用於希望用戶進行簡單'是''否'確認的場景。

在 PowerShell 的 GUI 中,它看起來像這樣。

在 PowerShell 中確認切換提示

在 PowerShell 中使用 if 語句進行確認提示

您也可以使用 if 語句並提供條件以便用戶繼續。例如,以下命令詢問用戶是否希望繼續。

命令:

$confirmation = Read-Host "Do you want to continue?"
if ($confirmation -eq 'y') {
    Write-Host "Welcome to DelftStack."
}

輸出:

Do you want to continue?: y
Welcome to DelftStack.

Read-Host cmdlet 用於從用戶那裡讀取輸入並將輸入存儲在變數 $confirmation 中。if 語句檢查用戶的輸入($confirmation)是否等於'y',如果為真,則執行指定的操作。

這種方法為自定義顯示給用戶的消息及其選擇後發生的事件提供了額外的選項。

在 PowerShell 中使用 PromptForChoice() 方法進行確認提示

在 PowerShell 中,您還可以使用 PromptForChoice() 方法提示用戶確認。它顯示一個對話框,允許用戶從一組選項中選擇一個選項並根據其選擇繼續執行操作。

以下命令是一個示例,提示用戶進行是/否輸入。我們定義了 $title$question$choices 以便確認提示。

命令:

$title = 'Confirm'
$question = 'Do you want to continue?'
$choices = '&Yes', '&No'

$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
    Write-Host 'Your choice is Yes.'
}
else {
    Write-Host 'Your choice is No.'
}

這行代碼 $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1) 使用 PromptForChoice() 方法向用戶顯示一個對話框。在 if 語句中,它檢查存儲在 $decision 變數中的值是否等於 0$choices 數組中第一個選項的索引)。

第一次輸出:

Confirm
Do you want to continue?
[Y] Yes  [N] No  [?] Help (default is "N"): Yes
Your choice is Yes.

在第一次輸出中,用戶選擇了"是"。因此,輸出"您的選擇是是。"被打印出來。

第二次輸出:

Confirm
Do you want to continue?
[Y] Yes  [N] No  [?] Help (default is "N"): No
Your choice is No.

在第二次輸出中,用戶選擇了"否",因此括號內的第二條命令將被執行。因此,"您的選擇是否。"被打印出來。

這種方法提供了更具互動性和自定義的確認體驗,適合於複雜的場景。

如果您在 PowerShell 的 GUI 版本中運行上述腳本,您可以查看確認對話框。

在 PowerShell 中的 PromptForChoice 確認提示

在 PowerShell 中使用基於函數的方法進行確認提示

您可以創建包含確認邏輯的自定義 PowerShell 函數。這使您能夠將確認邏輯封裝在函數中並在腳本中重用它們。

命令:

Function Confirm-Action {
    param (
        [string]$Message,
        [scriptblock]$Action
    )
    $confirmation = Read-Host $Message
    if ($confirmation -eq 'y') {
        & $Action
    }
}

Confirm-Action -Message "Do you want to continue?" -Action {
    Write-Host "Action executed."
}

Function Confirm-Action { ... } 行定義了一個名為 Confirm-Action 的 PowerShell 函數。該函數接受兩個參數:$Message$Action

這行 $confirmation = Read-Host $Message 使用 Read-Host cmdlet 提示用戶輸入並將輸入存儲在變數 $confirmation 中。$Message 參數指定顯示給用戶的消息。

if 語句檢查存儲在 $confirmation 中的用戶輸入是否等於字符'y'

輸出:

Do you want to continue?: y
Action executed.

當您運行命令時,它會提示用戶確認("您想繼續嗎?"),並在用戶選擇'y'時執行動作,顯示消息"操作已執行"

結論

在 PowerShell 中,確認提示對用戶控制和安全至關重要。在本文中,我們探討了四種實現確認提示的方法;-Confirm 開關簡化了基本的'是/否'提示,使用 Read-Hostif 語句允許自定義,PromptForChoice() 方法提供了先進的互動選項,以及使用基於函數的方法創建包含確認邏輯的自定義函數,確保腳本中用戶交互的高效和安全。

我們希望這個教程能幫助您了解如何在 PowerShell 中提示用戶進行'是/否'確認。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website