Public/Get-LabSnippet.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function Get-LabSnippet
{
    [CmdletBinding()]
    param
    (
        [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [string[]]
        $Name = '*',

        [string]
        $Description,

        [ValidateSet('Sample', 'Snippet', 'CustomRole')]
        [string]
        $Type = '*',

        [string[]]
        $Tag,

        [switch]
        $Syntax
    )

    process
    {
        foreach ($snippetName in $Name)
        {
            $scriptblockName = 'AutomatedLab.{0}.{1}' -f $Type, $snippetName

            $block = Get-PSFScriptblock -Name $scriptblockName -Description $Description -Tag $Tag
            $parameters = $block.ScriptBlock.Ast.ParamBlock.Parameters.Name.VariablePath.UserPath
            if ($parameters)
            {
                $block | Add-Member -NotePropertyName Parameters -NotePropertyValue $parameters -Force
            }

            if ($Syntax -and $block)
            {
                foreach ($blk in $block)
                {
                    $flatName = $blk.Name -replace '^AutomatedLab\..*\.'
                    "Invoke-LabSnippet -Name $($flatName) -LabParameter @{`r`n`0`0`0`0$($blk.Parameters -join `"='value'`r`n`0`0`0`0")='value'`r`n}`r`n"
                }
                continue
            }

            $block
        }
    }
}