PowerShell中的PassThru参数是什么 3


上周Lili和Yaqi分别问过PowerShell中的PassThru是什么,用来做什么? 今天主要讨论一下PowerShell中的PassThru参数。

首先看下PowerShell内置的命令中,那些命令包含了PassThru的定义了:

Get-Command | where { $_.Definition -like "*PassThru*" } | select Name

Name
----
Add-Computer
Add-Content
Add-History
Add-Member
Add-PSSnapin
Add-Type
Clear-ItemProperty
Clear-Variable
Compare-Object
Copy-Item
Copy-ItemProperty
Disable-PSBreakpoint
Enable-PSBreakpoint
Export-Alias
Import-Alias
Import-Module
Move-Item
Move-ItemProperty
New-Alias
New-ModuleManifest
New-Variable
Pop-Location
Push-Location
Remove-Computer
Remove-PSSnapin
Rename-Item
Rename-ItemProperty
Restart-Service
Resume-Service
Set-Acl
Set-Alias
Set-Content
Set-Item
Set-ItemProperty
Set-Location
Set-Service
Set-TraceSource
Set-Variable
Start-Process
Start-Service
Stop-Job
Stop-Process
Stop-Service
Suspend-Service

总过有44条命令。(测试的PowerShell为2.0版本,操作系统为windows7)那这44条命令主要涉及那些方面呢?可以分析命令的名词后缀:

PS C:\> Get-Command | where { $_.Definition -like "*PassThru*" } | select Noun -Unique

Noun
----
Computer
Content
History
Member
PSSnapin
Type
ItemProperty
Variable
Object
Item
PSBreakpoint
Alias
Module
ModuleManifest
Location
Service
Acl
TraceSource
Process
Job

让我们挑选三条命令进行分析。

  • Start-Process
    默认通过Start-Process打开记事本,在PowerShell的控制台上没有输出。但是如果加上了PassThru参数时,就会输出新打开的记事本的信息,并且用户可以将该进程信息保存到变量中,以进行下一步操作,例如输出进程的启动时间,通过管道关闭该进程。
    PS C:\> Start-Process notepad
    PS C:\> Start-Process notepad -PassThru
    
    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
    -------  ------    -----      ----- -----   ------     -- -----------
         60       7     2284       6644    77     0.03   3664 notepad
    
    
    PS C:\> $notepad=Start-Process notepad -PassThru
    PS C:\> $notepad.StartTime
    
    2013年1月12日 23:29:03
    
    
    PS C:\> $notepad | Stop-Process
    
  • Copy-Item
    默认通过Copy-Item拷贝文件时,如果不出错,在PowerShell控制台上没有输出。但是如果使用了PassThru参数,就会输出拷贝后目标文件的文件信息,并且是一个System.IO.FileInfo,有了这个对象就可以对目标文件进行下一步操作了。
    PS C:\> Copy-Item C:\softset.ini d:\softset.ini
    PS C:\> Copy-Item C:\softset.ini d:\softset.ini -PassThru
    
    
        目录: D:\
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---          2013/1/2      1:51         70 softset.ini
    
    
    PS C:\> $file=Copy-Item C:\softset.ini d:\softset.ini -PassThru
    PS C:\> $file.GetType().FullName
    System.IO.FileInfo
    PS C:\> $file.FullName
    D:\softset.ini
    PS C:\> $file.Attributes
    Archive
    PS C:\> $file | Remove-Item
    
  • Import-Module
    默认通过Import-Module导入PowerShell模块定义时,在PowerShell控制台上没有输出。但是使用了PassThru参数,就会输出该模块定义的信息,并且可以将模块保存,进行下一步操作了。
    PS C:\> Import-Module AppLocker
    PS C:\> Import-Module AppLocker -PassThru
    
    ModuleType Name                      ExportedCommands
    ---------- ----                      ----------------
    Manifest   AppLocker                 {Set-AppLockerPolicy, New-AppLockerPolicy, Test-AppLockerPolicy,
    
    
    PS C:\> $appLocker=Import-Module AppLocker -PassThru
    PS C:\> $appLocker.Description
    Powershell AppLocker Module
    PS C:\> $appLocker.Guid
    
    Guid
    ----
    9dafd409-67de-4108-8ee9-73cd61f5b7bf

好了,事不过三,现在稍微总结一下,使用PowerShell 中的PassThru参数可以将那些新创建的或者经过更新的对象由默认的隐藏变成输出或返回,以便进行下一步操作,体现的正是PowerShell的灵活性。
是不是所有的命令都包含了PassThru参数?不是,PassThru参数在PowerShell命令中的定义中是可选的,不是像ErrorAction那一类通用的参数,所以倒有几分像Confirm,WhatIf参数。

本文链接: https://www.pstips.net/powershell-passthru-parameter.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 Mooser Lee 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

3 条评论 “PowerShell中的PassThru参数是什么

  • 阿里piapia

    # pwershell交流群
    # 群号=183173532
    # powershell在脚本人眼中玄黄,陌生,不屑一顾。powershell人在程序猿中洪荒,遭鄙视。
    # 每个powershell热血的人都应该拿起手中的$,砸死他们!所以:
    # 我们需要懂powershell的精英,需要拿的出手的成品软件,更需要解决方案。
    # 尽管群内人不多,尽管群友都挺菜,但这是世界上最大的关注powershell的【中国人员群组】
    # 来吧牛人,让我们携手,搞出点东西来。。。

    我不知道这网站是个人网站,还是多人网站。我想带着我的群,加入贵站,成为你网站的合作群,可以不?
    或者有什么条件?