[英]Parsing Parameters of a Powershell Script. How to do this from C#?
param(
[string]$name,
[string]$template
)
我有一個帶有以下參數的powershell腳本。 我正在從C#執行這個powershell腳本。 我想解析腳本並獲取參數。 將來腳本可能會更改,參數計數也會更改。
var str = @"param([string]$name,[string]$template)";
var matches = Regex.Matches(str, "\\[(?<type>\\w+)\\]\\$(?<name>\\w+)").OfType<Match>()
.Select(m => new
{
name = m.Groups["name"].Value,
type = m.Groups["type"].Value
});
foreach (var m in matches)
{
// check m.name and m.type
}
我將使用PowerShell Parser類。 為方便起見,以下示例在PowerShell中,但可以在C#中輕松完成:
PS> $str = 'param([string]$name,[string]$template)'
PS> $ast = [System.Management.Automation.Language.Parser]::ParseInput($str, [ref]$null, [ref]$null)
PS> $ast.ParamBlock.Parameters.Name.Extent.Text
$name
$template
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.