Благодаря Матиасу Р. Йессену у меня есть решение ниже:
foreach($scriptFile in Get-ChildItem .\Test\ -Filter *.ps1){
# Parse file
$AST = [System.Management.Automation.Language.Parser]::ParseFile($scriptFile.FullName,[ref]$null,[ref]$null)
$scriptName = $scriptFile.Name.Replace(".", "_");
# Look for invocations of `.DoWork()`
$DoWorkExpressions = $AST.FindAll({ param($p)
$p -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
$p.Name -eq 'DoSomething'
}, $true)
if($DoWorkExpressions.Count -gt 0){
# There's a call to .DoWork() in $scriptFile
# Do further AST analysis or execute the file
$newFunctionName = $scriptName + '_' + $DoWorkExpressions[0].Name;
$newFunction = 'function ' + $newFunctionName + $DoWorkExpressions[0].Body.Extent.Text;
# invoke part
Write-Output $newFunction
Invoke-Expression $newFunction
$result = &$newFunctionName 'bob'
Write-Output "${newFunctionName} 'bob' = $result"
}
}