Write-Warning

写入警告消息。

语法

Default (默认值)

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

说明

Write-Warning cmdlet 向 PowerShell 主机写入警告消息。 对警告的响应取决于用户 $WarningPreference 变量的值以及 WarningAction 常见参数的使用。

示例

示例 1:编写警告消息

此命令显示消息“WARNING:这只是测试警告”。

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 公共参数,其值为“查询”指示系统在命令显示警告时提示用户。

有关 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”。 生成的错误消息列出了有效值。