Skip to content

Instantly share code, notes, and snippets.

@bmoore-msft
Created August 27, 2018 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoore-msft/80ab6a70ee1f26c87881ed36e914fd3a to your computer and use it in GitHub Desktop.
Save bmoore-msft/80ab6a70ee1f26c87881ed36e914fd3a to your computer and use it in GitHub Desktop.
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