有没有一种简单的方法来解析 powershell 脚本文件中的参数
param(
[string]$name,
[string]$template
)
Run Code Online (Sandbox Code Playgroud)
我已经开始阅读该文件,想知道是否有更好的方法,也许通过 help/man 命令?
class PowerShellParameter {
public string Name;
public string Type;
public string Default;
}
string[] lines = File.ReadAllLines(path);
bool inparamblock = false;
for (int i = 0; i < lines.Length; i++) {
if (lines[i].Contains("param")) {
inparamblock = true;
} else if (inparamblock) {
new PowerShellParameter(...)
if (lines[i].Contains(")")) {
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
至少有两种可能性。第一个(恕我直言更好):使用Get-Command
:
# my test file
@'
param(
$p1,
$p2
)
write-host $p1 $p2
'@ | Set-content -path $env:temp\sotest.ps1
(Get-Command $env:temp\sotest.ps1).parameters.keys
Run Code Online (Sandbox Code Playgroud)
所有会员请查看
Get-Command $env:temp\sotest.ps1 | gm
#or
Get-Command $env:temp\sotest.ps1 | fl *
Run Code Online (Sandbox Code Playgroud)
另一种(更难的方法)是使用正则表达式
[regex]::Matches((Get-Help $env:temp\sotest.ps1), '(?<=\[\[-)[\w]+') | select -exp Value
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3971 次 |
最近记录: |