Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active October 3, 2024 16:14
Show Gist options
  • Select an option

  • Save ninmonkey/a89677cc28ff2080025de5ae3fa97cbf to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/a89677cc28ff2080025de5ae3fa97cbf to your computer and use it in GitHub Desktop.
Parsing a powershell script using the AST
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