Last active
October 18, 2018 11:02
-
-
Save markwragg/aab1f5b1fe7a38a80c419eaee7d428b5 to your computer and use it in GitHub Desktop.
Find all cmdlets being used in a specified script by using Abstract Syntax Trees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$AST = [System.Management.Automation.Language.Parser]::ParseFile( | |
(Resolve-Path './Some-File.ps1'), | |
[ref]$null, | |
[ref]$Null | |
) | |
$CommandElements = ($AST.FindAll( {$args[0] -is [System.Management.Automation.Language.CommandAst]}, $true)).CommandElements | |
($CommandElements | Where-Object {$_.StringConstantType -eq 'BareWord'}).Value | Sort-Object -Unique |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment