Deploying A Virtual Machine Using Azure PowerShell

Azure PowerShell provides a set of cmdlets that use the Azure Resource Manager model for managing your Azure resources. You can use it in your browser with Azure Cloud Shell, or you can install it on your local machine and use it in any PowerShell session. It is optimized for managing and administering Azure Resources from the command line, and for building automation scripts that work against the Azure Resource Manager.

In this article, we will help you to create a Virtual Machine using Azure PowerShell. You can launch the Cloud Shell by clicking the icon on the top of the Azure portal.


On launching for the first time, you will get a window at the bottom to select the environment. Here, I am selecting PowerShell for this demo. (If you have already used for CLI 2.0, you can just switch to PowerShell from the top left corner of the Cloud Shell window, you need not create the storage account again).


Now, it will prompt you to create a storage account to persist your files from the Shell session.


So, what happens here is like when you spin up a Cloud Shell, Microsoft is going to stand up or initiate a container for you in the background, and you're going to be able to interact with that container and it's going to have the CLI installed in it. Clicking on the "Create Storage" button will build a resource group with the storage account inside it, and then, it'll launch a container for us. After initializing, you are ready to go.


Let us create a resource group first. The syntax is as follows.


This will give you a status on completion and you can check the resource group from the portal.


Now, we have to add resources to this group that is a Virtual Machine. For this demo, I am using a Windows Server 2016 Data Center. Unlike the CLI, you have to do more to get this going, you have to build the dependencies.

First, we can create the networking resources, i.e., a vnet, subnet, a public IP address, and a network interface using the following script.

  1. # Create a subnet configuration  
  2. $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24  
  3.   
  4. # Create a virtual network  
  5. $vnet = New-AzureRmVirtualNetwork -ResourceGroupName PSDemoRG -Location southeastasia -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig  
  6.   
  7. # Create a public IP address and specify a DNS name  
  8. $pip = New-AzureRmPublicIpAddress -ResourceGroupName PSDemoRG -Location southeastasia -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"  
  9.   
  10. $Interface = New-AzureRmNetworkInterface -Name ServerInterface -ResourceGroupName PSDemoRG -Location southeastasia -SubnetId $vNet.Subnets[0].Id -PublicIpAddressId $pip.Id  


Now, let us create the virtual machine using the following code snippet. This configuration includes the settings that are used when deploying the virtual machine such as a virtual machine image, size, and authentication configuration. When running this step, you are prompted for credentials. The values that you enter are configured as the username and password for the virtual machine.

  1. # Define a credential object  
  2. $cred = Get-Credential -Message "Enter a username and password for the virtual machine."  
  3.   
  4. # Create a virtual machine configuration  
  5. $vmConfig = New-AzureRmVMConfig -VMName PSDemoVM -VMSize Standard_DS2 | Set-AzureRmVMOperatingSystem -Windows -ComputerName DemoVM -Credential $cred | Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $Interface.Id  
  6.   
  7. New-AzureRmVM -ResourceGroupName PSDemoRG -Location southeastasia -VM $vmConfig  

And if you check the recourse group, now you can see the resources created.


You can connect to the VM using RDP file and giving user credentials. Once you have logged in to the Azure VM, you can use a single line of PowerShell to install IIS and enable the local firewall rule to allow web traffic. Open a PowerShell prompt and run the following command.

  1. Install-WindowsFeature -name Web-Server -IncludeManagementTools  


By using the public IP address, you can visit the default IIS Welcome page.


Up Next
    Ebook Download
    View all
    Learn
    View all