austoonz
2/14/2020 - 3:42 PM

Find all AWS Tools for PowerShell commands in a given file

This function will use the PowerShell Parser, and the Get-AWSCmdletName cmdlet (found in AWS.Tools.Common, AWSPowerShell or AWSPowerShell.NetCore) to find and output all AWS Cmdlets from a script. This can be used to assist with migrations to the modular AWS Tools for PowerShell.

function Get-AWSCommandsFromFile {
    [CmdletBinding()]
    param (
        [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
        $FilePath
    )

    $awsCommands = @{}

    $tokens = [System.Management.Automation.PSParser]::Tokenize((Get-Content -Path $FilePath -Raw), [ref]$null)
    foreach ($token in $tokens) {
        if ($token.Type -ne 'Command') { continue }

        $awsCmdlet = Get-AWSCmdletName -CmdletName $token.Content
        if ($awsCmdlet) {
            $key = '{0}{1}' -f $awsCmdlet.ModuleName, $awsCmdlet.CmdletName
            if ($awsCommands.ContainsKey($key)) { continue }

            $null = $awsCommands.Add($key, $awsCmdlet)
        }
    }

    $awsCommands.Values | Sort-Object -Property ModuleName, CmdletName
}