Skip to content

Instantly share code, notes, and snippets.

@taddison
Created July 5, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taddison/b4085e83a2bb6b254a82588576f2a040 to your computer and use it in GitHub Desktop.
Save taddison/b4085e83a2bb6b254a82588576f2a040 to your computer and use it in GitHub Desktop.
Find the first tag value for a describe block in a pester test file
$text = Get-Content .\src\SQLChecks\Tests\Databases.tests.ps1
$ast = [Management.Automation.Language.Parser]::ParseInput($text, [ref]$null, [ref]$null)
$ast.FindAll({
param($t)
$t -is [System.Management.Automation.Language.CommandAst] -and
$t.CommandElements[0].Value -eq "Describe"
}, $true) | ForEach-Object {
$_.CommandElements[3].Value
}
# Inspiration:
# - https://sqldbawithabeard.com/2018/01/15/using-the-ast-in-pester-for-dbachecks/
# - https://blogs.technet.microsoft.com/heyscriptingguy/2012/09/26/learn-how-it-pros-can-use-the-powershell-ast/
# - https://stackoverflow.com/questions/39909021/parsing-powershell-script-with-ast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment