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
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
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.
- # Create a subnet configuration
- $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
- # Create a virtual network
- $vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location EastUS `
- -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
- # Create a public IP address and specify a DNS name
- $pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location EastUS `
- -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"
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.
- # Create an inbound network security group rule for port 3389
- $nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
- -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
- -DestinationPortRange 3389 -Access Allow
- # Create an inbound network security group rule for port 80
- $nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp `
- -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
- -DestinationPortRange 80 -Access Allow
- # Create a network security group
- $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location EastUS `
- -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb
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.
- # Create a virtual network card and associate with public IP address and NSG
- $nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location EastUS `
- -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.
- #Define a credential object
- $cred = Get - Credential# Create a virtual machine configuration
- $vmConfig = New - AzureRmVMConfig - VMName myVM - VMSize Standard_DS2 | `
-
- Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `
- Set - AzureRmVMSourceImage - PublisherName MicrosoftWindowsServer - Offer WindowsServer `
-
- -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id
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
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
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>
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.