Checking Cmdlet Availability and Script Compatibility (Part 3)

by Mar 14, 2019

Not all PowerShell cmdlets ship with PowerShell. Many are part of 3rd party modules which in turn ship when you install certain software, or use certain Windows versions.

In the previous part we produced a function that dumps all external commands found in a script. With just a little extra effort, this can be turned into a useful compatibility report: any cmdlet originates from a module, and any module with a name starting with “Microsoft.PowerShell” ships with PowerShell. Any other modules are specific to certain Windows versions or 3rd party extensions.

Check out this function:

function Get-ExternalCommand
{
    param
    (
        [Parameter(Mandatory)][string]
        $Path
    )
    function Get-ContainedCommand
    {
        param
        (
            [Parameter(Mandatory)][string]
            $Path,

            [string][ValidateSet('FunctionDefinition','Command')]
            $ItemType
        )

        $Token = $Err = $null
        $ast = [Management.Automation.Language.Parser]::ParseFile($Path, [ref] $Token, [ref] $Err)

        $ast.FindAll({ $args[0].GetType().Name -eq "${ItemType}Ast" }, $true)

    }


$functionNames = Get-ContainedCommand $Path -ItemType FunctionDefinition | 
  Select-Object -ExpandProperty Name
   
    $commands = Get-ContainedCommand $Path -ItemType Command 
    $commands | Where-Object {
      $commandName = $_.CommandElements[0].Extent.Text
      $commandName -notin $functionNames
      } | 
      ForEach-Object { $_.GetCommandName() } |
      Sort-Object -Unique |
      ForEach-Object { 
    $module = (Get-Command -name $_).Source
    $builtIn = $module -like 'Microsoft.PowerShell.*'

    [PSCustomObject]@{
        Command = $_
        BuiltIn = $builtIn
        Module = $module
    }
    }
}

Here is a sample report from a PowerShell script listing all external cmdlets and whether they are part of PowerShell or come from external modules:

 
PS> Get-ExternalCommand -Path $Path


Command                BuiltIn Module                         
-------                ------- ------                         
ConvertFrom-StringData    True Microsoft.PowerShell.Utility   
Get-Acl                   True Microsoft.PowerShell.Security  
Get-ItemProperty          True Microsoft.PowerShell.Management
Get-Service               True Microsoft.PowerShell.Management
Get-WmiObject             True Microsoft.PowerShell.Management
New-Object                True Microsoft.PowerShell.Utility   
out-default               True Microsoft.PowerShell.Core      
Test-Path                 True Microsoft.PowerShell.Management
Where-Object              True Microsoft.PowerShell.Core      
write-host                True Microsoft.PowerShell.Utility   
 
PS> 
 

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!