Write-Warning
寫入警告訊息。
語法
Default (預設值)
Write-Warning
[-Message] <String>
[<CommonParameters>]
Description
Write-Warning Cmdlet 會將警告訊息寫入 PowerShell 主機。 警告的回應取決於使用者 $WarningPreference 變數的值,以及使用 WarningAction 一般參數。
範例
範例 1:撰寫警告訊息
此命令會顯示「警告:這隻是測試警告」訊息。
Write-Warning "This is only a test warning."
範例 2:將字串傳遞至 Write-Warning
這個指令顯示您可以使用管線運算子 (|) 將字串傳送至 Write-Warning。
您可以將字串儲存在變數中,如此命令所示,或直接使用管線將字串傳送至 Write-Warning。
$w = "This is only a test warning."
$w | Write-Warning
範例 3:設定$WarningPreference變數並寫入警告
這個範例顯示 $WarningPreference 命令上 Write-Warning 變數值的效果。
PS> $WarningPreference
Continue
PS> Write-Warning "This is only a test warning."
This is only a test warning.
PS> $WarningPreference = "SilentlyContinue"
PS> Write-Warning "This is only a test warning."
PS> $WarningPreference = "Stop"
PS> Write-Warning "This is only a test warning."
WARNING: This is only a test warning.
Write-Warning: The running command stopped because the preference variable "WarningPreference" or common parameter is set to Stop: This is only a test warning.
第一個命令會顯示 $WarningPreference 變數的預設值,也就是 Continue。 因此,當您撰寫警告時,會顯示警告訊息並繼續執行。
當您變更 $WarningPreference 變數的值時,Write-Warning 命令的效果會再次變更。
SilentlyContinue 的值會隱藏警告。
Stop 的值會顯示警告,然後停止執行命令。
如需 $WarningPreference 變數的詳細資訊,請參閱 about_Preference_Variables。
範例 4:設定 WarningAction 參數並寫入警告
此範例顯示 WarningActionWrite-Warning 命令上通用參數的效果。 您可以使用 WarningAction 一般參數搭配任何 Cmdlet 來判斷 PowerShell 如何響應該命令所產生的警告。
WarningAction 一般參數只會針對該特定命令覆寫 $WarningPreference 的值。
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"):
此命令會使用 Write-Warning Cmdlet 來顯示警告。
WarningAction Common 參數,其值為 [詢問] 會指示系統在命令顯示警告時提示使用者。
如需 WarningAction 一般參數的詳細資訊,請參閱 about_CommonParameters。
參數
-Message
指定警告訊息。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 味精 |
參數集
(All)
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
您可以使用管線將包含警告的字串傳送至此 Cmdlet。
輸出
None
此 Cmdlet 不會傳回任何輸出。 它只會寫入警告數據流。
備註
$WarningPreference 變數的預設值為 Continue,其中會顯示警告,然後繼續執行 命令。 若要判斷喜好設定變數的有效值,例如 $WarningPreference,請將它設定為隨機字元字串,例如 “abc”。 產生的錯誤訊息會列出有效的值。