如何调用带有未知参数的脚本
How to call a script with unknown parameters
我有一个脚本可以调用其他人管理的其他脚本。它本质上是一个 CI/CD 脚本,使用户能够利用管道。
我现在 运行 遇到的问题是我希望此调用脚本实现几个新参数。但是,旧脚本并不总是实现这些参数。
如果我调用他们的未实现参数的脚本,我会收到错误消息“无法找到与参数名称 'newparameter' 匹配的参数”。
有没有办法动态传入一个参数,在参数不存在的情况下也不会失败?如果他们不实施,我不介意。这是他们不需要使用的奖励参数。
或者,我可以为自定义 .ps1 脚本执行类似 Get-Command 的操作,以获取接受的参数列表吗?有了它,我可以在传递之前确认参数已实现。
这可能会帮助您入门,您可以使用 解析器 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 to get the AST, then you can use the .FindAll
method to filter for all FunctionDefinitionAst。
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
我有一个脚本可以调用其他人管理的其他脚本。它本质上是一个 CI/CD 脚本,使用户能够利用管道。
我现在 运行 遇到的问题是我希望此调用脚本实现几个新参数。但是,旧脚本并不总是实现这些参数。
如果我调用他们的未实现参数的脚本,我会收到错误消息“无法找到与参数名称 'newparameter' 匹配的参数”。
有没有办法动态传入一个参数,在参数不存在的情况下也不会失败?如果他们不实施,我不介意。这是他们不需要使用的奖励参数。
或者,我可以为自定义 .ps1 脚本执行类似 Get-Command 的操作,以获取接受的参数列表吗?有了它,我可以在传递之前确认参数已实现。
这可能会帮助您入门,您可以使用 解析器 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 to get the AST, then you can use the .FindAll
method to filter for all FunctionDefinitionAst。
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