Write-Warning
寫下警告訊息。
語法
Default (預設值)
Write-Warning
[-Message] <String>
[<CommonParameters>]
Description
指令 Write-Warning 長會寫警告訊息給 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 參數並撰寫警告
此範例展示了 WarningAction 公共參數對指令 Write-Warning 的影響。 你可以用 WarningAction 的通用參數搭配任何 cmdlet,來判斷 PowerShell 如何回應該指令產生的警告。
WarningAction 的共同參數會覆蓋該指令中 only 的值$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 的共用參數,值為 Inquire,指示系統在指令顯示警告時提醒使用者。
欲了解更多關於 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」。 產生的錯誤訊息列出了有效值。