Functions/Public/Publish-AzureStorageBlob.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
function Publish-AzureStorageBlob {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
    )

    ### Load the Windows Forms .NET Assembly
    Add-Type -AssemblyName System.Windows.Forms;
    $OpenFileDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog;
    $OpenFileDialog.Multiselect = $true;
    $DialogResult = $OpenFileDialog.ShowDialog();
    if ($DialogResult -ne [System.Windows.Forms.DialogResult]::OK) {
        throw 'Please select one or more files to upload to the Microsoft Azure blob storage container.';
    }

    ### Prompt user to select a Microsoft Azure Storage Account to upload the file(s) to
    $StorageAccount = Get-AzureRmStorageAccount | Select-Object -Property StorageAccountName, Location, AffinityGroup | Out-GridView -OutputMode Single;
    if (!$StorageAccount) {
        throw 'No Azure Storage Account was selected. Please re-run the command and select a Microsoft Azure Storage Account.';
    }
    $StorageKey = Get-AzureRmStorageAccountKey -StorageAccountName $StorageAccount.StorageAccountName;
    $StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccount.StorageAccountName -StorageAccountKey $StorageKey.Primary;
    
    ### Prompt user to select a Microsoft Azure Blob Container (inside the Storage Account) to upload the file(s) to
    $ContainerList = Get-AzureStorageContainer -Context $StorageContext;
    $Container = $ContainerList | Out-GridView -Title 'Please select a target Microsoft Azure blob storage container.' -OutputMode Single;

    ### If the user does not select a container, then prompt the user to create a new one.
    if (!$Container) {
        $ContainerName = Read-Host -Prompt 'Please enter a container name';
        if ($ContainerList.Name -notcontains $ContainerName) {
            $Container = New-AzureStorageContainer -Context $StorageContext -Name $ContainerName;
        }
        else {
            throw 'Container named {0} already exists in Storage Account {1}. Please re-run the command and use a different container name.' -f $ContainerName, $StorageAccount.StorageAccountName;
        }
    }

    foreach ($File in $OpenFileDialog.FileNames) {
        Write-Verbose -Message ('Uploading file {0} to {1}/{2}' -f $File, $StorageAccount.StorageAccountName, $Container.Name);
        if ($PSCmdlet.ShouldProcess($File, 'Upload to Azure blob storage')) {
            try {
                Set-AzureStorageBlobContent -Context $StorageContext -Container $Container.Name -File $File -ErrorAction Stop;
            }
            catch {
                Write-Warning -Message ('Failed to copy {0} to Azure blob container' -f $File);
            }
        }
    }
}