Public/Export-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function Export-LabSnippet
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [string]
        $Name,

        [string[]]
        $DependsOn,

        [switch]
        $MetaData
    )

    process
    {
        $schnippet = Get-LabSnippet -Name $Name
        $type = $schnippet.Name.Split('.')[1]
        $useAzure = Get-PSFConfigValue -FullName AutomatedLab.Recipe.UseAzureBlobStorage
        $location = Get-PSFConfigValue -FullName AutomatedLab.Recipe.SnippetStore
        $filePath = Join-Path -Path $location -ChildPath "$($schnippet.Name).ps1"
        $metaPath = Join-Path -Path $location -ChildPath "$($schnippet.Name).psd1"
        

        if ($useAzure)
        {
            if (-not (Get-Command -Name Set-AzStorageBlobContent -ErrorAction SilentlyContinue))
            {                
                Write-ScreenInfo -Type Error -Message "Az.Storage is missing. To use Azure, ensure that the module Az is installed."
                return
            }

            if (-not (Get-AzContext))
            {
                Write-ScreenInfo -Type Error -Message "No Azure context. Please follow the on-screen instructions to log in."
                $null = Connect-AzAccount -UseDeviceAuthentication
            }

            $account = Get-PSFConfigValue -FullName AutomatedLab.Recipe.AzureBlobStorage.AccountName
            $rg = Get-PSFConfigValue -FullName AutomatedLab.Recipe.AzureBlobStorage.ResourceGroupName
            $container = Get-PSFConfigValue -FullName AutomatedLab.Recipe.AzureBlobStorage.ContainerName
            
            if (-not $account -or -not $rg -or -not $container)
            {
                Write-ScreenInfo -Type Error -Message "Unable to upload to storage account, parameters missing. You provided AzureBlobStorage.AccountName as '$account', AzureBlobStorage.ResourceGroupName as '$rg' and AzureBlobStorage.ContainerName as '$container'"
                return
            }
            
            $ctx = (Get-AzStorageAccount -ResourceGroupName $rg -Name $account -ErrorAction SilentlyContinue).Context

            if (-not $ctx)
            {
                Write-ScreenInfo -Type Error -Message "Unable to establish storage context with account $account. Does it exist?"
                return
            }

            
            if (-not (Get-AzStorageContainer -Name $container -Context $ctx))
            {
                $null = New-AzStorageContainer -Name $container -Context $ctx
            }
        }

        if (-not $useAzure -and -not (Test-Path -Path $location))
        {
            $null = New-Item -Path $location -ItemType Directory -Force
        }

        if (-not $useAzure -and -not $MetaData.IsPresent)
        {
            Set-Content -Path $filePath -Value $schnippet.ScriptBlock.ToString() -Encoding Unicode -Force
        }
        
        if ($useAzure -and -not $MetaData.IsPresent)
        {
            $tmpFile = New-TemporaryFile
            Set-Content -Path $tmpFile.FullName -Value $schnippet.ScriptBlock.ToString() -Encoding Unicode -Force
            $null = Set-AzStorageBlobContent -File $tmpFile.FullName -Container $container -Blob "$($type)/$($schnippet.Name).ps1" -Context $ctx
            $tmpFile | Remove-Item
        }

        $metaContent = @"
@{
    Name = '$Name'
    Type = '$Type'
    Tag = @(
        $(($Tag | ForEach-Object {"'$_'"}) -join ",")
    )
    DependsOn = @(
        $(($DependsOn | ForEach-Object {"'$_'"}) -join ",")
    )
    Description = '$($Description.Replace("'", "''"))'
}
"@
 

        if ($useAzure)
        {
            $tmpFile = New-TemporaryFile
            Set-Content -Path $tmpFile.FullName -Value $metaContent -Encoding Unicode -Force
            $null = Set-AzStorageBlobContent -File $tmpFile.FullName -Container $container -Blob "$($type)/$($schnippet.Name).psd1" -Context (Get-AzStorageAccount -ResourceGroupName $rg -Name $account).Context
            $tmpFile | Remove-Item
        }
        else
        {
            $metaContent | Set-Content -Path $metaPath -Force
        }
    }
}