Create A Virtual Machine Using PowerShell Cmdlets

Introduction

In this article, I will explain about creating a virtual machine in an Azure, using Windows PowerShell. Let’s see what Windows PowerShell is all about. PowerShell is used to create and manage Azure resources with the command lines. From this article, PowerShell commands will help to build a virtual machine of Windows Server 2016 and some of the command lines will not be marked since it is blocking some content views of the images. 

Prerequisites

  • An active Azure subscription.
  • Windows PowerShell 2.0 SDK and can be downloaded from the link https://www.microsoft.com/en-in/download/details.aspx?id=2560

Steps to create a virtual machine through PowerShell commands

Step 1

Open Windows PowerShell.

Step 2

Login into your Azure account by the command line given below.

Login-AzureRmAccount

Azure
Azure

Step 3

Now we need to create a resource group in Azure so that we can manage Azure resources, which are being deployed and managed. The resource group creation is followed by the command line.

New-AzureRmResourceGroup -Name myResourceGroup -Location EastUS

Azure

Make sure that you can change the resource group name and the location name wherever it's being used in the command lines.

Create network resource

Step 4 

Creating a virtual network and subnet with public IP

The network connectivity resource must be provided for the virtual machine and the command is given below.

  1. # Create a subnet configuration  
  2. $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24  
  3. # Create a virtual network  
  4. $vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location EastUS `  
  5. -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig  
  6. # Create a public IP address and specify a DNS name  
  7. $pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location EastUS `  
  8. -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"  

 Azure

Azure
Azure
Azure

Step 5 

Creating a network security group

The virtual machine is secured by the network security group by using the inbound and outbound rules so that the inbound rule is created for port 3389 by the remote connection and we need to create an inbound rule for the port 80, which allows the incoming traffic Web and is represented by the command line.

  1. # Create an inbound network security group rule for port 3389  
  2. $nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `  
  3. -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `  
  4. -DestinationPortRange 3389 -Access Allow  
  5. # Create an inbound network security group rule for port 80  
  6. $nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp `  
  7. -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `  
  8. -DestinationPortRange 80 -Access Allow  
  9. # Create a network security group  
  10. $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location EastUS `  
  11. -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb  

Azure
Azure

Step 6 

Creating a network card for VM

The network card for the VM is the VM to a subnet and public IP address. It is represented by PowerShell command.

  1. # Create a virtual network card and associate with public IP address and NSG  
  2. $nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location EastUS `  
  3. -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id   

Step 7 

Creating a virtual machine

Now we need to configure the VM. This configuration includes the settings, which remains significant for virtual machine. The password along with the username is provided for VM.

  1. #Define a credential object  
  2. $cred = Get - Credential# Create a virtual machine configuration  
  3. $vmConfig = New - AzureRmVMConfig - VMName myVM - VMSize Standard_DS2 | `  
  4.   
  5. Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `  
  6. Set - AzureRmVMSourceImage - PublisherName MicrosoftWindowsServer - Offer WindowsServer `  
  7.   
  8. -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id  

Azure
Azure

Step 8

Now create a virtual machine with new Azure remote VM and PowerShell command is given below.

New-AzureRmVM -ResourceGroupName myResourceGroup -Location EastUS -VM $vmConfig

Azure

Step 9

Connect to VM by the remote desktop connection after the deployment.

Make a note of the VM IP address and login to VM by providing the IP address for an IP address PowerShell command.

Get-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress

Azure
Azure

For the remote desktop session of VM, we need to replace the IP address with Public IP of VM and PowerShell command is given below.

mstsc /v:<publicIpAddress>

Azure
Azure

Step 10

After login to VM, we can install IIS and enable the local Firewall, open PowerShell in VM and type PowerShell command if you need.

Install-WindowsFeature -name Web-Server -IncludeManagementTools

Thus the virtual machine is created, using the Windows PowerShell. I hope, this article will be useful to help you create a virtual machine, using PowerShell, and thanks for reading.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now