查看PowerShell命令的实现


PowerShell的命令一般有两种实现:一种直接定义在.NET的程序集中,另外一种是使用PowerShell脚本实现的。后者可以直接看到其实现,有时候学习系统命令的实现,可能会给我们写脚本也带来灵感。

查看那些命令定义在Dll中

PS> Get-Command | where { $_.DLL -ne $null }

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Add-Computer                                       Microsoft.PowerShell.Management
Cmdlet          Add-Content                                        Microsoft.PowerShell.Management
Cmdlet          Add-History                                        Microsoft.PowerShell.Core
Cmdlet          Add-Member                                         Microsoft.PowerShell.Utility
Cmdlet          Add-PSSnapin                                       Microsoft.PowerShell.Core
……

查看Add-Computer命令的Dll

PS> (Get-Command Add-Computer).DLL
I:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0__31bf3856ad364e35\Micr
osoft.PowerShell.Commands.Management.dll

Get-Command返回的对象有一个属性Definition,如果该命令的实现为脚本,则可以查看具体实现。例如我想查看命令Unlock-BitLocker的实现,使用:

(Get-Command Unlock-BitLocker).Definition

输出结果:

    [CmdletBinding(
        SupportsShouldProcess=$true,
        HelpUri="http://go.microsoft.com/fwlink/?LinkID=264892")]
    Param(
          [Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true)]
          [ValidateNotNullOrEmpty()]
          [string[]]
          $MountPoint,

          #
          # Password Protector
          #

          [Parameter(Mandatory = $true, ParameterSetName="OnlyPasswordParameterSet")]
          [Alias("pw")]
          [System.Security.SecureString]
          $Password,

          #
          # Recovery Password Protector
          #

          [Parameter(Mandatory = $true, ParameterSetName="OnlyRecoveryPasswordParameterSet")]
          [ValidateNotNullOrEmpty()]
          [Alias("rp")]
          [String]
          $RecoveryPassword,

          #
          # Recovery Key Protector
          #

          [Parameter(Mandatory = $true, ParameterSetName="OnlyRecoveryKeyParameterSet")]
          [ValidateNotNullOrEmpty()]
          [Alias("rk")]
          [String]
          $RecoveryKeyPath,

          #
          # Ad Account Or Group Protector
          #

          [Parameter(Mandatory = $true, ParameterSetName="OnlyAdAccountOrGroupParameterSet")]
          [System.Management.Automation.SwitchParameter]
          $AdAccountOrGroup
    )

    process
    {
       Write-Debug "Begin Unlock-BitLocker($MountPoint)"

       #########
       # ValidMountPoint is a subset of the elements of MountPoint array.
       # If MountPoint array contains an element that is not a valid mount point then
       # the mount point is not part of ValidMountPoint
       # Only those BitLockerVolume structures are returned that are part of ValidMountPoint
       #
       # If "-whatif" is used then ValidMountPoint is always $null
       #########
       [string[]]$ValidMountPoint = $null

       for($i=0; $i -lt $MountPoint.Count; $i++)
       {
            $BitLockerVolumeInternal = Get-BitLockerVolumeInternal -MountPoint $MountPoint[$i]
            if (!$BitLockerVolumeInternal)
            {
                $m = $MountPoint[$i]
                Write-Debug "The following operation failed: Get-BitLockerVolumeInternal -MountPoint $m"
                continue
            }

            Write-Debug ("MountPoint: " + $BitLockerVolumeInternal.MountPoint)

            if ($pscmdlet.ShouldProcess($BitLockerVolumeInternal.MountPoint))
            {
                if ($PsCmdlet.ParameterSetName -eq "OnlyPasswordParameterSet")
                {
                    $Result = Unlock-PasswordInternal $BitLockerVolumeInternal.MountPoint $Password
                }
                elseif ($PsCmdlet.ParameterSetName -eq "OnlyRecoveryPasswordParameterSet")
                {
                    $Result = Unlock-RecoveryPasswordInternal $BitLockerVolumeInternal.MountPoint $RecoveryPassword
                }
                elseif ($PsCmdlet.ParameterSetName -eq "OnlyRecoveryKeyParameterSet")
                {
                    $Result = Unlock-RecoveryKeyInternal $BitLockerVolumeInternal.MountPoint $RecoveryKeyPath
                }
                elseif ($PsCmdlet.ParameterSetName -eq "OnlyAdAccountOrGroupParameterSet")
                {
                    $Result = Unlock-AdAccountOrGroupInternal $BitLockerVolumeInternal.MountPoint
                }

                if ($Result -ne 0)
                {
                    Write-Debug "Unlock-BitLocker failed for $BitLockerVolumeInternal.MountPoint"
                    continue
                }

                $ValidMountPoint = $ValidMountPoint + $MountPoint[$i]
            }
        } 

        Write-Debug "ValidMountPoint: $ValidMountPoint"

        if ($ValidMountPoint)
        {
            $BitLockerVolume = Get-BitLockerVolume -MountPoint $ValidMountPoint

            $BitLockerVolume
        }
        else
        {
            Write-Debug "No valid mount point was provided that can be unlocked"
        }

        Write-Debug "End Unlock-BitLocker. Return $BitLockerVolume"
    }
×用微信扫描并分享
本文链接: https://www.pstips.net/view-definition-of-cmdlets.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

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

发表评论

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