Condividi tramite


Registering and launching a VM from a vhd file

Once a vhd file is uploaded or migrated to the target blob container, you may be wondering how to register and launch the VM. Here is the powershell script which I use. This method doesn't require sysprep.

Disclaimer: Note that there are no warranties and these and other scripts are shared for personal use only. If you do find any improvements or bugs, please provide comments and feedback.

Steps: Launch Powershell ISE. Copy and paste the script below. Replace the highlighted text in red with the correct names before executing the script.


Login-AzureRMAccount
Select-AzureRmSubscription -SubscriptionName SubscriptionName
$resourceGroupName = "ResourceGroupName"
$location = "australiaeast" #this is an example location for Australia East region
$vmName = "TestVM" # Specify the VM name
$virtualNetwork = "VNet1" # Specify the virtual network name
$publicIPAddressName = "TestVMPIP"
$networkInterfaceName = "TestVMNIC"
$vmSize = "Standard_A1"
# Specify the OS disk
$diskName = $vmName  #suggestion is to use the VM name
$osDiskUri = '*https://storage1.blob.core.windows.net/vhds/TestVMOS.vhd*' #you can obtain this from properties of the vhd file using Storage Explorer
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetwork -ResourceGroupName $resourceGroupName
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -AllocationMethod Dynamic -Location $location -Name $publicIPAddressName
$nic = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Location $location -Name $networkInterfaceName -PublicIpAddressId $pip.Id -SubnetId $vnet.Subnets[0].Id
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskname -VhdUri $osDiskUri -CreateOption Attach -Windows
$result = New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm


 

Disclaimer: The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.