寫入警告訊息。

語法

Write-Warning [-Message] <string> [<CommonParameters>]

描述

Write-Warning Cmdlet 會將警告訊息寫入 Windows PowerShell 主機。對警告的回應取決於使用者的 $WarningPreference 變數值和一般的 WarningAction 參數的使用。

參數

-Message <string>

指定警告訊息。

必要?

true

位置?

1

預設值

接受管線輸入?

true (ByValue)

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

System.String

您可經由管道將包含警告的字串輸出至 Write-Warning。

輸出

Write-Warning 只會寫入警告資料流。它不會產生任何其他輸出。

附註

$WarningPreference 變數的預設值為 "Continue",即在顯示警告後再繼續執行命令。若要判斷喜好設定變數 (例如 $WarningPreference) 的有效值,請將它設為任意字元的字串,例如 "abc"。產生的錯誤訊息將會列出有效值。

範例 1

C:\PS>write-warning "This is only a test warning."

描述
-----------
這個命令將會顯示「警告:This is only a test warning.」。






範例 2

C:\PS>$w = "This is only a test warning."

C:\PS> $w | write-warning

描述
-----------
這個範例會示範您可以使用管線運算子 (|) 將字串傳送給 Write-Warning。您可以將該字串儲存到變數,如這個命令所示,或經由管道將該字串直接輸出至 Write-Warning。






範例 3

C:\PS>$warningpreference

Continue

C:\PS> write-warning "This is only a test warning."
This is only a test warning.

C:\PS> $warningpreference = "SilentlyContinue"

C:\PS> write-warning "This is only a test warning."
C:\PS>

C:\PS> $warningpreference = "Stop"

C:\PS> write-warning "This is only a test warning."
WARNING: This is only a test message.
Write-Warning : Command execution stopped because the shell variable "WarningPreference" is set to Stop.
At line:1 char:14
+ write-warning <<<<  "This is only a test message."

描述
-----------
這個命令示範 $WarningPreference 變數值在 Write-Warning 命令上的效果。

第一個命令會顯示 $WarningPreference 變數的預設值,即 "Continue"。因此,當您寫入警告時,會出現警告訊息,但執行會繼續。

當您變更 $WarningPreference 變數的值時,Write-Warning 命令的效果會再次變更。值為 "SilentlyContinue" 會隱藏訊息。值為 "Stop" 會顯示警告後再停止命令的執行。

如需 $WarningPreference 變數的詳細資訊,請參閱 about_Preference_Variables。






範例 4

C:\PS>write-warning "This is only a test warning." -warningaction Inquire

WARNING: This is only a test warning.

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"):

描述
-----------
這個命令示範一般的 WarningAction 參數在 Write-Warning 命令上的效果。您可以使用一般的 WarningAction 參數搭配任何 Cmdlet,以決定 Windows PowerShell 對那個命令所產生之警告的回應方式。一般的 WarningAction 參數只會覆寫該特定命令的 $WarningPreference 值。

這個命令會使用 Write-Warning Cmdlet 顯示警告。值為 "Inquire" 的一般 WarningAction 參數會指示系統在命令顯示警告時提示使用者。

如需 WarningAction 一般參數的詳細資訊,請參閱 about_CommonParameters。






請參閱




目錄