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 参数并编写警告
此示例显示 WarningAction 公共参数对 Write-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”。 生成的错误消息列出了有效值。