Export/Public/New-StorageTables.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
function New-StorageTables {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        Creates Storage Tables and imports default data
    .DESCRIPTION
        ...
    #>

    param(
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupName,        
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceLocation,
        [Parameter(Mandatory = $true)]
        $StorageAccountContext,
        [Parameter(Mandatory = $true)]
        [Object[]]
        $Tables,
        [switch]
        $AsJob
    )
    function Add-Entity() {
        [CmdletBinding()]
        param
        (
            $Table,
            [Hashtable] $KeyValuePairs
        )
        Write-Verbose "Adding entry to storage table"
        $properties = @{ }
        foreach ($key in $KeyValuePairs.Keys) {
            if (($key.ToString() -ne "PartitionKey") -and ($key.ToString() -ne "RowKey")) {
                $properties.Add($key, $KeyValuePairs[$key])
            }
        }

        $params = @{
            Table          = $table
            PartitionKey   = $KeyValuePairs.PartitionKey
            RowKey         = $KeyValuePairs.RowKey
            property       = $properties
            UpdateExisting = $true
        }
        Add-AzTableRow @params | Out-Null
        Write-Verbose "Entry added"
    }
    foreach ($table in $Tables) {
        if (-not(Get-AzStorageTable -Name $table.TableName -Context $storageAccountContext -ErrorAction SilentlyContinue)) {
            Write-Verbose "Creating Storage Account Table $($table.TableName)..."
            $storageAccountTable = New-AzStorageTable -Name $table.TableName -Context $StorageAccountContext
            foreach ($valueEntry in $table.Values) {
                Add-Entity -Table $storageAccountTable.CloudTable -KeyValuePairs $valueEntry | Out-Null
            }
        }
    }
}