Skip to content

Instantly share code, notes, and snippets.

@dfch
Last active March 12, 2018 22:01
Show Gist options
  • Select an option

  • Save dfch/21a7dd47198b61ec2fd0 to your computer and use it in GitHub Desktop.

Select an option

Save dfch/21a7dd47198b61ec2fd0 to your computer and use it in GitHub Desktop.

Revisions

  1. dfch revised this gist Apr 26, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Get-Functions.ps1
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,8 @@ BEGIN
    $CurrentBlock = $_MyInvocation.MyCommand.ScriptBlock.Ast.$BlockName;
    foreach($Statement in $CurrentBlock.Statements)
    {
    $Extend = $Statement.Extent.ToString();
    if([String]::IsNullOrWhiteSpace($Statement.Name) -Or $Extend -inotmatch ('function\W+(?<name>{0})' -f $Statement.Name))
    $Extent = $Statement.Extent.ToString();
    if([String]::IsNullOrWhiteSpace($Statement.Name) -Or $Extent -inotmatch ('function\W+(?<name>{0})' -f $Statement.Name))
    {
    continue;
    }
  2. dfch revised this gist Apr 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Get-Functions.ps1
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #Requires -Version 3
    # http://d-fens.ch/2015/04/26/nobrainer-get-all-functions-in-a-powershell-script-file-via-ast
    # http://d-fens.ch/2015/04/26/nobrainer-enumerate-all-functions-in-a-powershell-script-file-via-ast
    [CmdletBinding(
    SupportsShouldProcess = $true
    ,
  3. dfch revised this gist Apr 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Get-Functions.ps1
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #Requires -Version 3
    # http://d-fens.ch/2015/04/26/nobrainer-get-all-functions-in-a-powershell-script-file-via-reflection
    # http://d-fens.ch/2015/04/26/nobrainer-get-all-functions-in-a-powershell-script-file-via-ast
    [CmdletBinding(
    SupportsShouldProcess = $true
    ,
  4. dfch created this gist Apr 26, 2015.
    84 changes: 84 additions & 0 deletions Get-Functions.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #Requires -Version 3
    # http://d-fens.ch/2015/04/26/nobrainer-get-all-functions-in-a-powershell-script-file-via-reflection
    [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)
    {
    $Extend = $Statement.Extent.ToString();
    if([String]::IsNullOrWhiteSpace($Statement.Name) -Or $Extend -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.
    #
    #