次の方法で共有


Using Microsoft Azure PowerShell and Portal to Move a VM from One Storage Account to Another

This is a detailed explanation and the steps that should be followed to move a VM from one storage account to another by using Microsoft Azure PowerShell and Portal.

The idea here is to copy the VM’s VHD from a source storage account to a destination storage account and create a VM from that VHD.

Procedure:

Before you start the procedure ensure Microsoft Azure PowerShell is installed, if not use the Microsoft Web Platform Installer, to download the executable file.

Once installed, open Microsoft Azure PowerShell and run the below commands:

1)      Add-AzureAccount

  • Enter the credentials to log in in to your Azure account

2)      Get-AzureSubscription

This command gives you the subscription name and the details about your current storage account. If there is no storage account associated, you can set up an account by using the below commands:

3)      Set-AzureSubscription -SubscriptionName <name from above output> -CurrentStorageAccountName <your storage account name>

4)      Create an object for your source VHD URL: (the location of your source VHD) — the below screen capture will help you locate the URL. (Refer Fig: 1)

$srcUri = “https://<your storage account name>.blob.core.windows.net/vhds/<location of your vhd>.vhd

Fig: 1

 

5)      Create an object for source storage account and storage key:

$srcStorageAccount = STORAGE ACCOUNT NAME

$srcStorageKey = PRIMARY ACCESS KEY

How to Locate STORAGE ACCOUNT NAME and PRIMARY ACCESS KEY

Go to the Management Portal >> Storage >> select the storage account as shown in Fig: 2.

Fig: 2

 

 Then, Click on Dashboard >> Manage Access Keys (Refer Fig: 3).

Fig: 3

 The above action will take you to a new window as shown below. Copy and paste the “storage account name = $srcStorageAccount” and “primary access key = $srcStorageKey” to execute the command.

Fig: 4

 

### Target Storage Account ###--> Before you run the below command you need to create the new storage account in Azure portal for your target location—(unless you have already created one)

 

6)      Create an object for your destination storage account

$destStorageAccount = "<Destination storage account name>"

$destStorageKey = "Storage key for destination storage account” you can get this information by following the steps mentioned in "How to Locate STORAGE ACCOUNT NAME and PRIMARY ACCESS KEY"

7)      The below Azure PowerShell commands can be copy pasted one by one:

 

###THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT  WARRANTY OF  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT  LIMITED TO THE IMPLIED WARRANTIE-S OF MERCHANTABILITY AND/OR FITNESS  FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INAB-ILITY TO USE, OR   RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. ###

 

### Create the source storage account context ### I am specifying a context object to access the source storage account. This allows you to copy VHDs from Windows Azure storage without opening up permissions to everyone.###

$srcContext = New-AzureStorageContext  –StorageAccountName $srcStorageAccount -StorageAccountKey $srcStorageKey 

 ### Create the destination storage account context ###

$destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccount -StorageAccountKey $destStorageKey

### Destination Container Name ### --- The below steps are needed to create a new container (unless if it’s already created)

$containerName = "vhds"

 ### Create the container on the destination ###

New-AzureStorageContainer -Name $containerName -Context $destContext

 

### Start the asynchronous copy - specify the source authentication with -SrcContext ###

$blob = Start-AzureStorageBlobCopy -srcUri $srcUri -SrcContext $srcContext -DestContainer $containerName -DestBlob "<new name of VHD in the destination>.vhd" -DestContext $destContext

 

To get the status of the copy you can run the below commands:

### Retrieve the current status of the copy operation ###

$status = $blob | Get-AzureStorageBlobCopyState

 ### Print out status ###

$status

Once the copy process is completed, you have to create a blob copy.

Steps to create a VM using the blob:

The VHD that is copied from the source is still in blob format, you will need to create VHD from that blob:

1)      To create the VHD—go to Virtual Machines >> Disks >> Click Create >> New Window >> Mention the Name of the New VHD >> Click on the VHD URL.

 

2)      You can see the storage account as shown below >> go to VHDs and you will see the .vhd name: “<new name of vhd that we copied>.vhd”

 

3)      Select the VHD and click open, this action will take you to the below screen. Click the checkbox “The VHD contains an operating system” if there is an operating system, ignore this step if the attached VHD is a data disk.

 

4)      Once the VHD is created you can create a new VM in the same cloud Service or a new cloud service under the new storage account:

5)       Select new >> Virtual Machine and From Gallery:

 

6)      Once done select My Disks and it will show the OS Disk that was copied—(If it’s a data disk, it will be ready for attaching to any VM in the region)

 

 

7)      The rest of the steps after selecting the OS disk is same as creating a new VM—If you need a new Cloud Service and new IP then choose the new Cloud service—if not then choose the same cloud service.

 

Please Note: If you are looking for a faster way to copy the VHDs from one storage account to another you can run the PowerShell script provided by Adam Conkle here:

https://gallery.technet.microsoft.com/Azure-Virtual-Machine-Copy-1041199c