Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copies the blob from a managed disk export to another storage account and creates a sasToken for marketplace publishing
param
(
[string]$sourceBlobUri,
[String]$StorageAccountName,
[string]$StorageAccountResourceGroupLocation,
[String]$StorageContainerName = 'vhds',
[String]$BlobName = 'marketplace.vhd'
)
#get Dest Storage context
if ($StorageAccountName -eq '') {
$StorageAccountName = 'stage' + ((Get-AzureRmContext).Subscription.Id).Replace('-', '').substring(0, 19)
}
$StorageAccount = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName})
# Create the storage account if it doesn't already exist
if ($StorageAccount -eq $null) {
$StorageAccountResourceGroupName = 'ARM_Deploy_Staging'
if ($StorageAccountResourceGroupLocation -eq '') {
throw "StorageAccountResourceGroupLocation parameter is required when creating a storage account."
}
New-AzureRmResourceGroup -Location "$StorageAccountResourceGroupLocation" -Name $StorageAccountResourceGroupName -Force
$StorageAccount = New-AzureRmStorageAccount -StorageAccountName $StorageAccountName -Type 'Standard_LRS' -ResourceGroupName $StorageAccountResourceGroupName -Location "$StorageAccountResourceGroupLocation"
}
New-AzureStorageContainer -Name $StorageContainerName -Context $StorageAccount.Context -ErrorAction SilentlyContinue *>&1
$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $StorageAccountResourceGroupName -Name $StorageAccountName).Key1
Start-AzureStorageBlobCopy -AbsoluteUri $sourceBlobUri -DestContext $storageAccount.Context -DestContainer $StorageContainerName -DestBlob $BlobName -Verbose #-Debug
Get-AzureStorageBlobCopyState -Blob marketplace.vhd -Context $storageAccount.context -Container vhds -WaitForComplete
$vhdUri = New-AzureStorageContainerSASToken -Container $StorageContainerName -FullUri -Permission rl -Context $storageAccount.Context -ExpiryTime (Get-Date).AddDays(10)
$vhdUri = $vhdUri.replace("?", "/$blobName"+"?")
Write-Host "URI for VHD: "$vhdUri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment