小编Bra*_*lin的帖子

使用AST解析PowerShell脚本

我正在尝试解析Pester脚本并从-Tag参数中提取值.任何人都知道如何使用[System.Management.Automation.PSParser]?我在想我必须遍历从中返回的令牌,[System.Management.Automation.PSParser]::Tokenize()但这看起来非常糟糕,并且考虑到-Tag可以以多种不同的格式给出值,这不是很实用.

在一天结束时,我希望返回一个包含Describe块名称的集合,以及该块的标记列表(如果有的话).

Name     Tags        
----     ----        
Section1 {tag1, tag2}
Section2 {foo, bar}  
Section3 {asdf}      
Section4 {}      
Run Code Online (Sandbox Code Playgroud)

以下是我正在使用的Pester测试示例.

describe 'Section1' -Tag @('tag1', 'tag2') {
    it 'blah1' {
        $true | should be $true
    }
}
describe 'Section2' -Tag 'foo', 'bar' {
    it 'blah2' {
        $true | should be $true
    }    
}
describe 'Section3' -Tag 'asdf'{
    it 'blah3' {
        $true | should be $true
    }
}
describe 'Section4' {
   it 'blah4' {
        $true …
Run Code Online (Sandbox Code Playgroud)

powershell parsing abstract-syntax-tree

5
推荐指数
1
解决办法
1590
查看次数

标签 统计

abstract-syntax-tree ×1

parsing ×1

powershell ×1