Learn about the PowerShell Abstract Syntax Tree (AST) – Part 3

This blog article is the third in a series of learning about the PowerShell Abstract Syntax Tree (AST). Be sure to read the other two if you haven’t already.

In this blog article, I’ll be specifically focusing on finding the AST recursively.

I’ll start off by storing the path to one of my functions in a variable.

I’ll store the AST in a variable and output what’s in the variable just to confirm that it contains the AST.

Now to return the AST recursively. Although this command appears to return the same results based on the following screenshot, it actually returns much more.

The following example gives a better idea of the results. Just returning the top level AST only returns one item, whereas querying the AST recursively returns 118 items, or 26 different unique types.

You’ll probably be overwhelmed by all of the AST that’s returned when querying it recursively. The solution is to narrow down the results. What are the AST type possibilities that can be used to narrow down the results?

A list of them can be retrieved by querying the System.Management.Automation.Language.ArrayExpressionAst .NET class. I ended up using a regular expression to cleanup the results and while I’ve seen others do this without a regex, they typically end up removing “Expression” from the list which makes it incomplete.

I’ll use one of the items (Variable) from the previous list to return only the AST information for the variables used in my function.

Finally, I’ll return a unique list of only the variables themselves that are used in the function.

As you can see, there’s a lot of power in using the AST.

In my next blog article, I’ll share and demonstrate functions that I’ve created that use the AST to help automate my script module build process.

µ

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: