Last active
October 3, 2024 16:14
-
-
Save ninmonkey/a89677cc28ff2080025de5ae3fa97cbf to your computer and use it in GitHub Desktop.
Parsing a powershell script using the AST
This file contains hidden or 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
| using namespace System.Management.Automation | |
| using namespace System.Management.Automation.Language | |
| $Tokens = $null | |
| $AstErrors = $Null | |
| $doc = [Parser]::ParseFile( | |
| <# fileName: #> $file, | |
| <# tokens : #> [ref] $tokens, | |
| <# errors : #> [ref] $AstErrors ) | |
| $query = $doc.FindAll({ | |
| param( [Ast]$Ast ) | |
| $Ast -is [CommandAst] | |
| }, <# recurse #> $true ) | |
| $query | Select * | Out-GridView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment