Feb 23 2019 02:02 PM - edited Feb 23 2019 02:03 PM
Original Article: https://wikiazure.com/compute/mover-vhd-hacia-azure-usando-powershell/
This is a guide for moving a VHD to Azure using PowerShell with Azure Resource Manager 5.1 module. Then in the Azure portal, we will create an image from the VHD, this will help us to be able to provision VMs from that image.
This guide is provided as an alternative solution for provisioning a KEMP LoadMaster virtual machine on an Azure subscription with the Enterprise Agreement in case there are limitations for the current region to provision the KEMP LoadMaster directly from the Azure marketplace.
Note: You can move an exported VHD from a local virtualization tool or from another cloud.
The use of managed disks for the new virtual machine simplifies the administration of the virtual machine and provides better availability when the virtual machine is placed in an availability set.
Move VHD to Azure using PowerShell and create an image object from the OS Disk
1. We will open Powershell on our local computer and access our Azure subscription through the command:
"AzureRM.Profile \ Login-AzureRmAccount"
2. To simplify the process we will use two variables: The name of the Azure subscription and the ID of the Azure subscription by executing the following commands (replace the name and ID of your subscription):
$Subscription =
"Your Subscription Name"
$SubscriptionId =
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3. We will select the current subscription as Default, for that we execute the following command:
"Get-AzureRmSubscription -SubscriptionId"
4
d
278909
-xxxx-xxxx-xxxx-xxxxxxxx
"-TenantId"
683
bfafa-xxxx-xxxx-a
787
-xxxxxxxxx
"| Set-AzureRmContext"
4. Proceed to create a Storage Account / Storage Account through the following command:
$storageAccount = New-AzureRmStorageAccount -ResourceGroupName
"DRendon"
`
-Name
"kempvdhtestingdr"
`
-SkuName Standard_LRS `
-Location
"eastus"
$ctx = $ storageAccount.Context
5. Now we are going to generate the container for our blob storage:
$containerName =
"kempvdhcontainerdr"
new-azurestoragecontainer -Name $ containerName -Context $ctx -Permission blob
6. Store in a variable the location of the VHD:
$localPath =
'C:\Users\daver\Downloads\VLM-trial\LoadMaster-VLM-Microsoft-HyperV\vlm2.vhd'
7. Assign a variable to name the VHD:
$vhdName =
'kempVhdtest.vhd'
8. Proceed to move the VHD to the blob storage:
$urlOfUploadedImageVhd = (
'https://kempvdhtestingdr.blob.core.windows.net/kempvdhcontainerdr'
+
'/'
+ $vhdName)
Add-AzureRmVhd -ResourceGroupName
"DRendon"
-Destination $ urlOfUploadedImageVhd `
-LocalFilePath $localPath -OverWrite
"Add-AzureRmVhd cmdlet uploads on-premises virtual hard disks, in .vhd file format, to a blob storage account as fixed virtual hard disks"
There are other optional parameters that we can make use of:
The VHD migration process will start right away and depending on the size it could take a couple of minutes:
Once the upload has been successful you should see something like this:
Now there are 2 options to create the image: from PowerShell or from the Azure Portal.
1. Let's create an image object and then store it in the variable $ imageConfig
$location =
'EastUS'
$imageConfig = New-AzureRmImageConfig -Location $ location
2. Then we are going to set the properties of the operating system disk in the object of the image using the Set-AzureRmImageOsDisk command:
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized `
-BlobUri $urlOfUploadedImageVhd
3. Now proceed to generate the image:
$image = New-AzureRmImage -ImageName $imageName -ResourceGroupName $resourceGroupName -Image $imageConfig
Once the migration process is completed, go to the storage account to verify that the VHD is correctly listed:
2. Now go create a new resource, search for "image" then click on the image as shown below:
3. Provide the parameters required to provision the image from the VHD as follows:
You will see a notification if the image was created correctly:
Now we can create the virtual machines from this image when we need it:
Here is the complete script on VHD migration with Azure Powershell 5.x, you can also download it from here: https://gallery.technet.microsoft.com/Move-your-VHD-to-Azure-e16f9fd6
AzureRM.Profile\Login-AzureRmAccount
$subscriptionName = "Your Subscription Name"
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$tenantId ="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$resourceGroupName = 'DRendon'
$location = 'EastUS'
$vhdName = 'kemp360central-v1.25.vhd'
$imageName = 'kempcentral25'
$containerName = "kempcentraldrtestcontainer"
$storageAccountName = "kempcentraldrtest"
Select-AzureRmSubscription -SubscriptionId $subscriptionId
#Select your default subscription on ARM
Get-AzureRmSubscription -SubscriptionId $subscriptionId -TenantId $tenantId | Set-AzureRmContext
#create new storage account
$storageAccount = New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -SkuName Standard_LRS -Location $location
$ctx = $storageAccount.Context
#create container
new-azurestoragecontainer -Name $containerName -Context $ctx -Permission blob
#set the local path from the vhd
$localPath = 'C:\Users\daver\Downloads\kemp360central\kemp360central-v1.25.vhd'
# set the url of the image and move the vhd, also use the -overwrite option since process might fail sporadically
# -overwrite solves the error Add-AzureRmVhd : The pipeline was not run because a pipeline is already running.
# Pipelines cannot be run concurrently
$urlOfUploadedImageVhd = ('https://'
+ $storageAccountName + '.blob.core.windows.net/' + $containerName + '/' + $vhdName)
Add-AzureRmVhd -ResourceGroupName $resourceGroupName -Destination $urlOfUploadedImageVhd `
-LocalFilePath $localPath -OverWrite
# Create a managed image from the uploaded VHD
$imageConfig = New-AzureRmImageConfig -Location $location
#set the managed disk from the image
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized `
-BlobUri $urlOfUploadedImageVhd
$image = New-AzureRmImage -ImageName $imageName -ResourceGroupName $resourceGroupName -Image $imageConfig
You should be able to identify what Linux OS type/version is running on your PC, install Guest Agent, generalize the OS before importing it to Azure Cloud.
Spanish (es-MX): https://social.technet.microsoft.com/wiki/contents/articles/52416.mover-vhd-hacia-azure-usando-power...