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

简介: 【2月更文挑战第7篇】$Error变量,对于PowerShell执行出现的错误会被写入到这个变量里面,加上时间的累积,这个变量的数据量就会非常大,我们平常在排查问题的时候需要对错误信息进行Debu调试,这个时候ErrorVariable 就可以解决这个问题,它的主要作用是把执行出现错误的信息输出到我们定义的变量里面去。


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

1、ErrorVariable 错误变量

上篇文章我们讲过$Error变量,对于PowerShell执行出现的错误会被写入到这个变量里面,加上时间的累积,这个变量的数据量就会非常大,我们平常在排查问题的时候需要对错误信息进行Debu调试,这个时候ErrorVariable 就可以解决这个问题,它的主要作用是把执行出现错误的信息输出到我们定义的变量里面去。

变量输出方式:覆盖的方式输出错误信息到变量;追加的方式输出错误信息到变量(自定义变量前面增加 + 号 )。

示例:移除一个不存在的 端午节2.txt命令如下:

remove-item 端午节2.txt -ErrorVariable CurrError
$CurrError  #输出错误信息
#再执行一遍
remove-item 端午节2.txt -ErrorVariable CurrError 
$CurrError  #输出错误信息 #发现错误信息没有新增为两条
# 给命令增加+号 实现错误信息追加
remove-item 端午节2.txt -ErrorVariable +CurrError
$CurrError #输出两条错误信息表示追加成功了

覆盖方式执行

image.png

追加命令

image.png

2、OutVariable 结果输出

OutVariable的功能和Tee-Object比较类似,把当前命令的执行结果输出到定义的变量当中去,然后在命令结果传递到管道的同时,把执行结果保存到PowerShell变量中去。

示例:

获取Logs目录的信息

Get-ChildItem

image.png

获取的Logs目录信息保存的输出变量LogList

Get-ChildItem -OutVariable LogList
#输出LogList变量
$LogList

image.png

3、OutBuffer 输出Buffer定义

确定在通过管道发送任何对象之前,缓冲区中要累积的对象数。 如果省略此参数,则会在生成对象时发送对象。

此参数定义在将任何对象向下传递管道之前要存储在输出缓冲区中的对象数。 默认情况下,对象会立即沿管道向下传递。

参数类型为Int32

官方示例

ForEach-Object 处理使用 Write-Host cmdlet 的块。 显示以 2 或 OutBuffer + 1的批次交替显示。

1..4 | ForEach-Object {
        Write-Host "$($_): First"; $_
      } -OutBuffer 1 | ForEach-Object {
                        Write-Host "$($_): Second" }

输出结果

1: First
2: First
1: Second
2: Second
3: First
4: First
3: Second
4: Second

4、PipelineVariable管道参数

PipelineVariable 允许访问使用此参数的命令传递到下一管道段的最新值。 管道中的任何命令都可以使用命名的 PipelineVariable 访问值。 当变量传递到下一个管道段时,该值将分配给变量。 这使得 PipelineVariable 比特定的临时变量更易于使用,可能需要在多个位置分配该临时变量。

使用 PipelineVariable 允许任何管道命令访问 (传递的管道值,并) 由上一个命令以外的命令保存。 管道命令可以在处理通过管道的下一项时访问从中传递的最后一个值。

示例

第一个Foreach-Object命令的结果通过管道传递到第二Foreach-Object个命令中,该命令显示 和 $_$temp当前值。

# Create a variable named $temp
$temp=8
Get-Variable temp
# Note that the variable just created isn't available on the
# pipeline when -PipelineVariable creates the same variable name
1..5 | ForEach-Object -PipelineVariable temp -Begin {
    Write-Host "Step1[BEGIN]:`$temp=$temp"
} -Process {
  Write-Host "Step1[PROCESS]:`$temp=$temp - `$_=$_"
  Write-Output $_
} | ForEach-Object {
  Write-Host "`tStep2[PROCESS]:`$temp=$temp - `$_=$_"
}
# The $temp variable is deleted when the pipeline finishes
Get-Variable temp

输出

Name                           Value
----                           -----
temp                           8
Step1[BEGIN]:$temp=
Step1[PROCESS]:$temp= - $_=1
        Step2[PROCESS]:$temp=1 - $_=1
Step1[PROCESS]:$temp=1 - $_=2
        Step2[PROCESS]:$temp=2 - $_=2
Step1[PROCESS]:$temp=2 - $_=3
        Step2[PROCESS]:$temp=3 - $_=3
Step1[PROCESS]:$temp=3 - $_=4
        Step2[PROCESS]:$temp=4 - $_=4
Step1[PROCESS]:$temp=4 - $_=5
        Step2[PROCESS]:$temp=5 - $_=5
Name                           Value
----                           -----
temp
相关文章
|
2月前
|
程序员
PowerShell系列(十一):PowerShell Cmdlet高级参数介绍(一)
【2月更文挑战第6篇】Verbose 参数主要用来显示函数执行过程中通过Write-Verbose写入的相关信息,如果命令执行当中有写入则会有反馈信息输出,反之则没有任何信息输出。输入如下命令
PowerShell系列(十一):PowerShell Cmdlet高级参数介绍(一)
|
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概念介绍
PowerShell系列(十三):PowerShell Cmdlet高级参数介绍(三)
【2月更文挑战第8篇】WarningAction参数和执行命令过程中的警告有关系,该参数就是在PowerShell命令执行过程中出现警告之后进行的操作,默认环境中存在WarningPreference参数定义命令执行过程中出现警告的操作,当然也可以出现警告的时候执行特殊的操作
|
2月前
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
79 0
|
8月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
62 0
|
11月前
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
11月前
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
132 0
|
应用服务中间件 nginx Windows
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案