Hello and welcome to another tutorial on using PowerShell with Azure in order to create a new Virtual Machine resource from an existing VHD.

From time to time, we might need to copy a Virtual Machine environment from our Hyper-V environment to Azure and create a new VM Instance. To complete this task we will use PowerShell, since we can automate it for several machines over time and it’s a nice template that we can keep in our script repository.

Table of Contents

    The following script does not use parameters; all the variables are declared within the script, but if you would like to change it, it’s easy to put the param() code at the top of the script.

    The script that we will be talking about is the following:

    $rgName = "MainResourceGroup"
    $subnetName = "MainSubnet"
    $location = "East US"
    $vnetName = "MainVirtualNetwork"
    $vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
    $ipName = "vm1-pip"
    $pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
    $nicName = "vm1-nic"
    $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
    $vmName = "vm1"
    $vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_DS1_V2"
    $vm = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
    $osDiskUri = "https://mainstorage.blob.core.windows.net/vhds/vm1-327423.vhd"
    $osDiskName = $vmName + "osDisk"
    $vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption Attach -Windows
    #Create the new VM
    New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm

    Now let us dissect it and see what each line does. Before we jump into the script itself, it is important to run the Login-AzureRMAccount command to connect to your Azure Subscription.

    The first four lines of the script contain the name of the Resource Group, Virtual Network Subnet, the Location and the name of the Virtual network.

    The $vnet variable holds the configuration with the Virtual Network which we will later attach to the VM configuration variable.

    The $ipName variable is the name of the Public IP of the VM.

    The $pip variable holds the Public IP object that we create using the New-AzureRmPublicIpAddress cmdlet.

    The $nicName variable holds the name of the Network Card for the VM.

    The $nic variable holds the Network Card object that we create using the New-AzureRmVmNetworkInterface.

    The $vmName variable holds the name of the VM.

    The $vmConfig variable holds the configuration of the new VM. To it we attach all the other resources, such as the Network Card, Disk and so forth.

    The $osDiskUri holds the location of our VHD in an Azure storage blob.

    The rest of the script adds the disk and the network card to the VM configuration.

    Once the configuration variable assignment is done, we run the New-AzureRmVM cmdlet, where we specify the Resource Group name, location and we attach the $vm variable that holds the VM configuration to the –VM parameter.

    Once we run this script, we should get a success output and then we can RDP to it and continue our configuration inside the OS.

    I hope this article helped you out and thanks reading it. Enjoy!

    Leave a Reply

    Your email address will not be published. Required fields are marked *