Samselvaprabu
Samselvaprabu

Reputation: 18187

How to find scriptmethod using AST in powershell?

I have the following script.

function Sample-function
{

    Write-host "I am function"
}

function Sample-ScriptMethod
{


    $obj = [pscustomobject]@{}

    $obj | Add-Member -MemberType ScriptMethod -Name sm1 -Value {

        Write-host "I am scriptmethod1"
    }
    Write-host "I am troubling"
    $obj | Add-Member -MemberType ScriptMethod -Name sm2 -Value {

        Write-host "I am scriptmethod2"
    }
    $obj
}

Using AST i am able to find the function names as below.

function GEt-functionNames
{
    Param
    (
        $filepath="C:\repo\UnMapStress2\g2\lib\Common\Windows\NWPSF\tests\lib\Common\IOOperation.psm1"
    )

    $AST = [System.Management.Automation.Language.Parser]::ParseFile($filepath,[ref]$null,[ref]$Null)

    # Returns function name 
    $AST.FindAll({$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]},$true) | foreach { $_.name}
}

Get-functionNames -filepath "C:\Users\Administrator\Desktop\ASTSample.ps1"

The output is

Sample-function
Sample-ScriptMethod

I also would like to find the scriptmethods in the file. The file has 2 scriptmethods sm1 and sm2.

How to find them in powershell ? Is AST has any method to find it?

Updated: I have tried to find using below way but unable to find

$filepath = "C:\Users\Administrator\Desktop\ASTSample.ps1"
$AST = [System.Management.Automation.Language.Parser]::ParseFile($filepath,[ref]$null,[ref]$Null)

    # Returns function name 
    $AST.FindAll({$args[0] -is [System.Management.Automation.Language.ScriptBlockExpressionAst]},$true) | foreach { $_.name}

Upvotes: 0

Views: 217

Answers (1)

Andy Schneider
Andy Schneider

Reputation: 8684

You are only selecting the Name property in your foreach at the end. While the type [System.Management.Automation.Language.FunctionDefinitionAst] may have a Name property, the [System.Management.Automation.Language.ScriptBlockExpressionAst] does not.

You can see your ScriptBlocks if you remove the foreach from your code.

# $AST = [System.Management.Automation.Language.Parser]::ParseFile($filepath,[ref]$null,[ref]$Null)
# $AST.FindAll({$args[0] -is [System.Management.Automation.Language.ScriptBlockExpressionAst]},$true) | fl


ScriptBlock : {

                      Write-host "I am scriptmethod1"
                  }
StaticType  : System.Management.Automation.ScriptBlock
Extent      : {

                      Write-host "I am scriptmethod1"
                  }
Parent      : Add-Member -MemberType ScriptMethod -Name sm1 -Value {

                      Write-host "I am scriptmethod1"
                  }

ScriptBlock : {

                      Write-host "I am scriptmethod2"
                  }
StaticType  : System.Management.Automation.ScriptBlock
Extent      : {

                      Write-host "I am scriptmethod2"
                  }
Parent      : Add-Member -MemberType ScriptMethod -Name sm2 -Value {

                      Write-host "I am scriptmethod2"
                  }

Upvotes: 2

Related Questions