Created
April 23, 2015 08:14
-
-
Save z2z/bf87f16109739b70b452 to your computer and use it in GitHub Desktop.
Migrate Azure VM image from one Subscription to another Subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://serverfault.com/a/655552/283258 | |
Select-AzureSubscription -SubscriptionName "EXISTING SUBSCRIPTION NAME" | |
#Get-AzureVM | |
$vmName = "YOUR_VM_NAME" | |
$serviceName = "CLOUD_SERVICE_NAME" | |
$destServiceName = "NEW_CLOUD_SERVICE_NAME" | |
$workingDir = (Get-Location).Path | |
$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 | |
Stop-AzureVM -ServiceName $serviceName -Name $vmName -Force | |
Select-AzureSubscription -SubscriptionName "NEW SUBSCRIPTION NAME" | |
$location = $sourceStorageAccount.Location | |
$destStorageAccount = Get-AzureStorageAccount | ? {$_.Location -eq $location} | select -first 1 | |
if ($destStorageAccount -eq $null) | |
{ | |
$destStorageName = "NEW_STORAGE_NAME" | |
New-AzureStorageAccount -StorageAccountName $destStorageName -Location $location | |
$ destStorageAccount = Get-AzureStorageAccount -StorageAccountName $destStorageName | |
} | |
$destStorageName = $destStorageAccount.StorageAccountName | |
$destStorageKey = (Get-AzureStorageKey -StorageAccountName $destStorageName).Primary | |
$sourceContext = New-AzureStorageContext -StorageAccountName $sourceStorageName ` | |
-StorageAccountKey $sourceStorageKey |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment