当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Write-Warning (Microsoft.PowerShell.Utility)

作者:精品下载站 日期:2024-12-14 02:02:10 浏览:15 分类:玩电脑

Write-Warning (Microsoft.PowerShell.Utility)


Write-Warning

模块 :Microsoft.PowerShell.Utility

写入警告消息。

句法

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

描述

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参数并写入警告

此示例显示 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 显示警告。值为 Inquire 的 WarningAction 公共参数指示系统在命令显示警告时提示用户。

有关 WarningAction 公共参数的更多信息,请参阅 about_CommonParameters。

参数

-Message

指定警告消息。

类型 :

String

别名:

Msg

位置:

0

默认值:

None

必需的:

True

接受管道输入:

True

接受通配符:

False

输入

字符串

您可以通过管道将包含警告的字符串传递给此 cmdlet。

输出

此 cmdlet 不返回任何输出。它仅写入警告流。

笔记

$WarningPreference 变量的默认值为 Continue,它显示警告,然后继续执行命令。要确定首选项变量(例如 $WarningPreference)的有效值,请将其设置为随机字符字符串,例如“abc”。生成的错误消息列出了有效值。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯