Skip to content

Instantly share code, notes, and snippets.

@don-smith
Created March 25, 2015 08:26
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 don-smith/0a5fce46db42b0936636 to your computer and use it in GitHub Desktop.
Save don-smith/0a5fce46db42b0936636 to your computer and use it in GitHub Desktop.
PowerShell script to copy a VM from one Azure subscription to another one
# Gratefully borrowed and edited from
# http://blogs.msdn.com/b/microsoft_press/archive/2014/01/29/from-the-mvps-copying-a-virtual-machine-from-one-windows-azure-subscription-to-another-with-powershell.aspx
# Source VM
$sourceSubscriptionName = "Name of the source subscription"
$destSubscriptionName = "Name of the destination subscription"
Get-AzureVM
$vmName = "name-of-source-vm"
$serviceName = "name-of-source-vm"
$destServiceName = "NameOfDestinationService"
$destStorageName = "destinationblobstorage"
$workingDir = (Get-Location).Path
$vNetName = "MyVNet"
Select-AzureSubscription -SubscriptionName $sourceSubscriptionName
$sourceVm = Get-AzureVM -ServiceName $serviceName -Name $vmName
$vmConfigurationPath = $workingDir + "\exportedVM.xml"
$sourceVm | Export-AzureVM -Path $vmConfigurationPath
$sourceOSDisk = $sourceVm.VM.OSVirtualHardDisk
$sourceDataDisks = $sourceVm.VM.DataVirtualHardDisks
$sourceStorageName = $sourceOSDisk.MediaLink.Host -split "\." | select -First 1
$sourceStorageAccount = Get-AzureStorageAccount -StorageAccountName $sourceStorageName
$sourceStorageKey = (Get-AzureStorageKey -StorageAccountName $sourceStorageName).Primary
# Destination VM
Select-AzureSubscription -SubscriptionName $destSubscriptionName
$location = $sourceStorageAccount.Location
$destStorageAccount = Get-AzureStorageAccount -StorageAccountName $destStorageName
if ($destStorageAccount -eq $null)
{
New-AzureStorageAccount -StorageAccountName $destStorageName -Label $destServiceName -Location $location
$destStorageAccount = Get-AzureStorageAccount -StorageAccountName $destStorageName
}
$destStorageName = $destStorageAccount.StorageAccountName
$destStorageKey = (Get-AzureStorageKey -StorageAccountName $destStorageName).Primary
$sourceContext = New-AzureStorageContext -StorageAccountName $sourceStorageName -StorageAccountKey $sourceStorageKey
$destContext = New-AzureStorageContext -StorageAccountName $destStorageName -StorageAccountKey $destStorageKey
# Copy the VMs
$allDisks = @($sourceOSDisk) + $sourceDataDisks
$destDataDisks = @()
foreach($disk in $allDisks)
{
$blobName = $disk.MediaLink.Segments[2]
$targetBlob = Start-CopyAzureStorageBlob -SrcContainer "vhds" -SrcBlob $blobName `
-DestContainer "vhds" -DestBlob $blobName `
-Context $sourceContext -DestContext $destContext -Force
Write-Host "Copying blob $blobName"
$copyState = $targetBlob | Get-AzureStorageBlobCopyState
while ($copyState.Status -ne "Success")
{
Write-Host "Completed $('{0:N2}' -f $percent)%"
sleep -Seconds 5
$copyState = $targetBlob | Get-AzureStorageBlobCopyState
}
If ($disk -eq $sourceOSDisk)
{
$destOSDisk = $targetBlob
}
Else
{
$destDataDisks += $targetBlob
}
}
# Register the new disks
Add-AzureDisk -OS $sourceOSDisk.OS -DiskName $sourceOSDisk.DiskName -MediaLocation $destOSDisk.ICloudBlob.Uri
foreach($currenDataDisk in $destDataDisks)
{
$diskName = ($sourceDataDisks | ? {$_.MediaLink.Segments[2] -eq $currenDataDisk.Name}).DiskName
Add-AzureDisk -DiskName $diskName -MediaLocation $currenDataDisk.ICloudBlob.Uri
}
# Copy over the VM configuration
# In case you need to rename the RoleName (what's visible in the Azure portal) of the destination VM
Write-Host "If necessary, edit the config file now, and then press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
Set-AzureSubscription -SubscriptionName $destSubscriptionName -CurrentStorageAccountName $destStorageName
$vmConfig = Import-AzureVM -Path $vmConfigurationPath
# Create the VM
New-AzureVM -ServiceName $destServiceName -ServiceLabel $destServiceName -Location $location -VMs $vmConfig -VNetName $vNetName -WaitForBoot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment