前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)

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

原创
作者头像
IT技术分享社区
发布2024-02-06 12:13:43
1520
发布2024-02-06 12:13:43
举报
文章被收录于专栏:运维运维

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

1、WarningAction参数

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

数据类型:枚举 Actionpreference

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

● Continue:出现警告后,显示警告信息的同时命令会继续执行。

● Inquire:出现警告后,会先询问操作者是否继续执行。

● SilentContinue:出现警告后,不显示警告信息,命令继续执行。

● Stop:出现警告后。立即停止执行后续的命令。

● Igonre:完全忽略警告,继续执行

● Suspend:预留作为后续使用

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

操作示例

代码语言:javascript
复制
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

运行效果如下图:

2、WarningVariable 出现警告后的变量

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

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

数据类型:字符串

示例

代码语言:javascript
复制
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>

3、Whatif 假设参数

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

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

示例

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

具体效果如下图

判断命令是否支持 Whatif

代码语言:javascript
复制
get-help Get-childitem -parameter whatif
get-help new-item -parameter whatif

具体输出效果如下图:

4、Confirm参数

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

代码语言:javascript
复制
get-help Get-childitem -parameter confirm
get-help new-item -parameter confirm

我正在参与2024腾讯技术创作特训营第五期有奖征文,快来和我瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、WarningAction参数
  • 2、WarningVariable 出现警告后的变量
  • 3、Whatif 假设参数
  • 4、Confirm参数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档