目录

1、WarningAction参数

2、WarningVariable 出现警告后的变量

3、Whatif 假设参数

4、Confirm参数


PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)-LMLPHP

今天给大家讲解PowerShell Cmdlet高级参数第三部分相关的知识,希望对大家学习PowerShell能有所帮助!

1、WarningAction参数

通过单词含义,就可以理解WarningAction参数和执行命令过程中的警告有关系,该参数就是在PowerShell命令执行过程中出现警告之后进行的操作,默认环境中存在WarningPreference参数定义命令执行过程中出现警告的操作,当然也可以出现警告的时候执行特殊的操作,这个时候可以使用WarningAction参数进行设置,从而覆盖默认的警告参数。

数据类型:枚举  Actionpreference

支持的操作方式主要有四种

  • Continue:出现警告后,显示警告信息的同时命令会继续执行。
  • Inquire:出现警告后,会先询问操作者是否继续执行。
  • SilentContinue:出现警告后,不显示警告信息,命令继续执行。
  • Stop:出现警告后。立即停止执行后续的命令。
  • Igonre:完全忽略警告,继续执行
  • Suspend:预留作为后续使用

说明:警告信息对于命令的排错调试还是非常有意义的,如果不是自动化的脚本建议保留。

操作示例

PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Inquire
警告: This is only a test warning.

确认
是否继续执行此操作?
[Y] 是(Y)  [A] 全是(A)  [H] 终止命令(H)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): Y
PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Ignore
PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Continue
警告: This is only a test warning.
PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Stop
警告: This is only a test warning.
Write-Warning : 已停止该运行的命令,因为首选项变量“WarningPreference”或通用参数设置为 Stop: This is only a test warning.
所在位置 行:1 字符: 1
+ Write-Warning "This is only a test warning." -WarningAction Stop
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Write-Warning], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ActionPreferenceStop,Microsoft.PowerShell.Commands.WriteWarningCommand

运行效果如下图:

PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)-LMLPHP

2、WarningVariable 出现警告后的变量

默认情况下PowerShell参数不会输出警告信息,也没有参数记录警告信息,如果你需要在命令执行过程当中记录警告信息,可以使用WarningVariable参数定义警告信息保存的变量。它的使用方式和ErrorVariable参数比较类似。

记录方式有两种:覆盖方式(默认方式)、追加方式 参数后需要增加 +  号 。

数据类型:字符串

示例

PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Continue -WarningVariable msg
警告: This is only a test warning.
PS D:\logs> $msg
This is only a test warning.
PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Continue -WarningVariable msg
警告: This is only a test warning.
PS D:\logs> $msg
This is only a test warning.
PS D:\logs> Write-Warning "This is only a test warning." -WarningAction Continue -WarningVariable +msg
警告: This is only a test warning.
PS D:\logs> $msg
This is only a test warning.
This is only a test warning.
PS D:\logs>

PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)-LMLPHP

3、Whatif 假设参数

此参数指定该 cmdlet 是否写入一条消息,该消息描述运行 cmdlet 的效果,而不实际执行任何操作。相当模拟操作,而不是实际执行命令。

通过该命令可以了解执行的步骤是否符合预期,针对动词命令(New、Update、Set等)支持WhatIf操作。默认情况下该参数不启用。

示例

  #当前命令通过增加-Whatif参数模拟创建文件创建
 New-item 测试文件.txt -Whatif 
 ls # 发现文件实际没有创建成功

具体效果如下图

PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)-LMLPHP

判断命令是否支持 Whatif

get-help Get-childitem -parameter whatif
get-help new-item -parameter whatif

具体输出效果如下图:

PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)-LMLPHP

4、Confirm参数

Confirm参数主要是用来确认命令执行操作的再确认,默认情况下命令执行过程是否需要再确认通过ConfirmPreference 参数的值决定,如果命令执行过程当中需要改变再确认选项可以使用Confirm参数替换ConfirmPreference 参数参数。

get-help Get-childitem -parameter confirm
get-help new-item -parameter confirm

PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)-LMLPHP

10-31 09:51