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

简介: 【2月更文挑战第8篇】WarningAction参数和执行命令过程中的警告有关系,该参数就是在PowerShell命令执行过程中出现警告之后进行的操作,默认环境中存在WarningPreference参数定义命令执行过程中出现警告的操作,当然也可以出现警告的时候执行特殊的操作

image.png


今天给大家讲解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

运行效果如下图:

image.png

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>

image.png

3、Whatif 假设参数

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

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

示例

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

具体效果如下图

image.png

判断命令是否支持 Whatif

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

具体输出效果如下图:

image.png

4、Confirm参数

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

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

image.png

相关文章
|
2月前
|
程序员
PowerShell系列(十一):PowerShell Cmdlet高级参数介绍(一)
【2月更文挑战第6篇】Verbose 参数主要用来显示函数执行过程中通过Write-Verbose写入的相关信息,如果命令执行当中有写入则会有反馈信息输出,反之则没有任何信息输出。输入如下命令
PowerShell系列(十一):PowerShell Cmdlet高级参数介绍(一)
|
2月前
|
存储
PowerShell系列(十二):PowerShell Cmdlet高级参数介绍(二)
【2月更文挑战第7篇】$Error变量,对于PowerShell执行出现的错误会被写入到这个变量里面,加上时间的累积,这个变量的数据量就会非常大,我们平常在排查问题的时候需要对错误信息进行Debu调试,这个时候ErrorVariable 就可以解决这个问题,它的主要作用是把执行出现错误的信息输出到我们定义的变量里面去。
|
2月前
|
文件存储
PowerShell系列(十):PowerShell CmdletPowerShell Cmdlet 参数详解
【2月更文挑战第5篇】强制类型参数使用比较频繁,基本上涉及新建、更新、配置等命令都需要针对特定的对应进行操作,所有需要强制输入一个参数来确认操作的对象是谁。
PowerShell系列(十):PowerShell CmdletPowerShell Cmdlet 参数详解
|
2月前
|
运维 Shell 数据处理
PowerShell系列(九)PowerShell Cmdlet概念介绍
【2月更文挑战第4篇】大家可能对cmd命令比较熟悉cmd就是Windows系统命令行窗口执行的命令,微软为了和传统的Shell命令区分,所以重新起了一个名词“Cmdlet”专门给PowerShell配套使用,Cmdlet可以理解为在PowerShell环境
PowerShell系列(九)PowerShell Cmdlet概念介绍
|
3月前
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
53 0
|
5月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
51 0
|
8月前
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
8月前
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
102 0
|
应用服务中间件 nginx Windows
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案