Recently I needed to check if a function was defined inside a script file. Reflection was my first thought, and PowerShell being a first class .NET citizen should be able to perform this. But as PowerShell is defined as an Abstract Syntax Tree (AST) it is even easier to achieve this.

Inside the AST every block (BEGIN, PROCESS, END) is an object you can investigate upon. And inside such a block you can see all statements and their extents. As every function has to start with the function keyword, you only have to see if the statement’s extent start with that keyword and check if that name corresponds to the statement’s name: ![String]::IsNullOrWhiteSpace($Statement.Name) -And $Extent -imatch ('function\W+(?{0})' -f $Statement.Name). That’s all to it.

In this Gist you see a full example that enumerates all functions inside the file:


#Requires -Version 3
# https://d-fens.ch/2015/04/26/nobrainer-enumerate-all-functions-in-a-powershell-script-file-via-ast
[CmdletBinding(
SupportsShouldProcess = $true
,
ConfirmImpact = 'Low'
,
DefaultParameterSetName = 'list'
)]
Param
(
[Parameter(Mandatory = $false, Position = 0, ParameterSetName = 'list')]
[Switch]
$ListAvailable = $true
)
BEGIN
{
function functionInBeginBlock()
{
return "functionInBeginBlock";
}
function getFunctions($_MyInvocation)
{
$OutputParameter = @();
foreach($BlockName in @("BeginBlock", "ProcessBlock", "EndBlock"))
{
$CurrentBlock = $_MyInvocation.MyCommand.ScriptBlock.Ast.$BlockName;
foreach($Statement in $CurrentBlock.Statements)
{
$Extent = $Statement.Extent.ToString();
if([String]::IsNullOrWhiteSpace($Statement.Name) -Or $Extent -inotmatch ('function\W+(?<name>{0})' -f $Statement.Name))
{
continue;
}
$OutputParameter += $Statement.Name;
}
}
return $OutputParameter;
}
}
PROCESS
{
function functionInProcessBlock()
{
return "functionInProcessBlock";
}
}
END
{
function functionInEndBlock()
{
return "functionInEndBlock";
}
function theAnswer()
{
return 42;
}
return getFunctions($MyInvocation);
}
##
#
#
# Copyright 2015 Ronald Rink, d-fens GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#

The output should look similar to this:

PS > .Get-Functions.ps1
functionInBeginBlock
getFunctions
functionInProcessBlock
functionInEndBlock
theAnswer

Note: you can certainly use this to list functions in other script files as well. You can use the ParseFile instead and continue from there.
You find more information on PowerShell and AST on MSDN and elsewhere. Two links to get started:

3 Comments »

  1. Stumped!? What file? What if I want the function list of file.ps1? All I have is .\Get-Function.ps1? where do I tell it to list the functions of file.ps1?

    • Hi Thomas. Thank you for your comment and question. The example in this article only parses itself: see the call to `return getFunctions($MyInvocation);`. You can adjust the code to your needs if you want to parse something else. Hope this clarifies things a bit. Regards, Ronald

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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