这可能会帮助您入门,您可以使用Parser Class
从脚本中获取所有函数及其参数,这个答案显示了最小的复制。我会留给你进一步调查。
鉴于 myScript.ps1 具有以下 3 个功能:
function ExampleFunc {
param([int] $param1 = 123, [string] $param2)
}
function ExampleFunc2 {
param([object] $param3, [switch] $param4)
}
function ExampleFunc3 ($param5, [hashtable] $param6 = @{foo = 'var'}) {
}
您可以使用ParseFile Method 来获取AST,然后您可以使用.FindAll method 过滤所有FunctionDefinitionAst,然后找到过滤所有ParameterAst 的所有参数。
using namespace System.Management.Automation.Language
$ast = [Parser]::ParseInput($myscript, [ref] $null, [ref] $null)
$ast.FindAll({ $args[0] -is [FunctionDefinitionAst] }, $true) | ForEach-Object {
$out = [ordered]@{ Function = $_.Name }
$_.FindAll({ $args[0] -is [ParameterAst] }, $true) | ForEach-Object {
$out['ParameterName'] = $_.Name.VariablePath
$out['Type'] = $_.StaticType
$out['DefaultValue'] = $_.DefaultValue
[pscustomobject] $out
}
} | Format-Table
对于myScript.ps1,上面的代码将导致以下结果:
Function ParameterName Type DefaultValue
-------- ------------- ---- ------------
ExampleFunc param1 System.Int32 123
ExampleFunc param2 System.String
ExampleFunc2 param3 System.Object
ExampleFunc2 param4 System.Management.Automation.SwitchParameter
ExampleFunc3 param5 System.Object
ExampleFunc3 param6 System.Collections.Hashtable @{foo = 'var'}
使用Get-Command 也可以实现相同的目的:
(Get-Command 'fullpath\to\myScript.ps1').ScriptBlock.Ast.FindAll({
... same syntax as before ... }, $true # or $false for non-recursive search
)