Scripts/Create-AzTableStorageAccountTable.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
param(
   [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of resource group is required"),
   [Parameter(Mandatory = $true)][string] $StorageAccountName = $(throw "Name of Azure storage account is required"),
   [Parameter(Mandatory = $true)][string] $TableName = $(throw "Name of Azure table is required"),
   [Parameter()][switch] $Recreate = $false
)

function Create-StorageAccountTable()
{
    [CmdletBinding()]
    param
    (
       [string][parameter(Mandatory = $true)] $ResourceGroupName,
       [string][parameter(Mandatory = $true)] $StorageAccountName,
       [string][parameter(Mandatory = $true)] $TableName,
       [bool][parameter()] $Recreate = $false
    )
    BEGIN
    {
        #Retrieve the storage account to which the table should be added
        Write-Host "Retrieving storage account ('$StorageAccountName') context..."
        $storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
        Write-Host "Storage account context has been retrieved."
        
        #Check if the table already exists
        Write-Host "Checking if the table '$TableName' already exists..."
        $table = Get-AzStorageTable -Name $TableName -Context $storageAccount.Context -ErrorAction Ignore

        if($table)
        {
            #Table already exists
            if($Recreate)
            {
                #Delete the table before re-creating
                Write-Host "Deleting existing table '$TableName' in the storage account '$StorageAccountName'..."
                Remove-AzStorageTable -Name $TableName -Context $storageAccount.Context
                Write-Host "Table '$TableName' has been removed"
                
                while(-not(Try-CreateTable -StorageAccount $storageAccount -TableName $TableName))
                {
                    Write-Host "Failed to create the table, retrying in 5 seconds..."
                    Start-Sleep -Seconds 5
                }
               
            }
            else
            {
                Write-Host "No actions performed, since the specified table ('$TableName') already exists in the storage account ('$StorageAccountName')."
            }
        }
        else
        {
            #Table does not exist
            Write-Host "Table '$TableName' does not exist yet"
            Try-CreateTable -StorageAccount $storageAccount -TableName $TableName
        }
    }
}

function Try-CreateTable()
{
    [CmdletBinding()]
    param
    (
        [object][parameter(Mandatory = $true)] $StorageAccount,
        [string][parameter(Mandatory = $true)] $TableName
    )
    BEGIN
    {
        try
        {
            Write-Host "Creating table '$TableName' in the storage account '$StorageAccountName'..."
            New-AzStorageTable -Name $TableName -Context $StorageAccount.Context -ErrorAction Stop
            Write-Host "Table '$TableName' has been created"

            return $true
        }
        catch
        {
            return $false
        }
    }
}

Create-StorageAccountTable -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -TableName $TableName -Recreate $Recreate