Overview
This article is about the creation of Virtual Machines in the Azure Cloud Service with the help of PowerShell commands. This will proceed through logging into the Azure portal using PowerShell and then, writing down the commands one by one to completely create a VM. As we know, a virtual machine will have a V-Net, Public-IP, storage account, machine size, Operating System, NSG, and Network Interface. We shall now see how we create all these just with the help of PowerShell commands.
Configuring PowerShell
First of all, to work with the Azure using PowerShell, you need to have the PowerShell module installed on your machine. For this, run the following command to download the latest version of Azure PowerShell module.
Get-Module -ListAvailable AzureRM
If you have the Azure PowerShell module installed, proceed to the next step.
Logging into Azure
Open PowerShell with the administrator rights and then try logging into the Azure portal running the below command.
Login-AzureRmAccount
Creating resource group
Now, create a new resource group for the VM you are going to create. The resource group is nothing but a virtual folder in which we can organize and maintain all our resources at one single cluster. This resource group will also have a location. Hence, we can decide the location of our services using the location of our RG. For this, run the below command.
New-AzureRmResourceGroup -Name myResourceGroup -Location EastUS
Creating V-Net, Subnet, and Public-IP
For creating a VM, we need V-Net, Subnet, and a Public-IP. Run these commands one by one to create each one of these services. You can even run all these commands at once. As the heading states, the below code will actually create a subnet, a V-net, and a Public-IP.
# 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)"
Creating an NSG with inbound rule
Now, we shall create a Network Security Group. The network security groups are used for maintaining control over the requests that are actually going to be received by our VM. The rule will be created for port 3389 which will actually allow the incoming RDP connections to the VM. We will also create a rule for the port 80 to allow the incoming web traffic. Now, run the following set of commands so that the rules will get created.
# 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
Creating a network card for the VM
The network card is created for connecting the VM with the subnet, NSG, and the Public-IP. Create the network card by running the below-given command.
Creating a Virtual Machine
Now, we have to configure our VM. Here, we will be choosing the size of the VM, the authentication credentials (username and password), and the type of virtual machine image that is actually needed. Now, run the below code by changing the username if needed.
# Define a credential object
$cred = Get-Credential
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_DS2 | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName CodeSizzler,
Now, we shall create the Virtual Machine by running the following command.
New-AzureRmVM -ResourceGroupName myResourceGroup -Location EastUS -VM
$vmConfig
Connecting to Virtual Machine
Wait for the VM to get deployed. Once the VM gets deployed, run the below-given command by adding your VM’s IP address to it.
Here, make a note of the IP address of your VM once it's deployed. You cannot connect to your VM without your IP address. To get the IP address, run the following command.
Get-AzureRmPublicIpAddress
If you want to specifically find the IP address of a VM, you can use the below command and give the specific details of the RG and VM name.
Get-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress
Now, copy down the IP address and run the below command by replacing your IP address. You will be getting an RDP asking for the username and password. Enter those credentials and login into the VM. This is how you create a VM through PowerShell and work with it.
mstsc /v:13.82.216.128
Deleting VM
You can delete the VM by either deleting the VM alone or you can even delete the entire RG so that all the resources that are created along with the VM will be deleted. To delete the VM, run the RG and run the given command.
Remove-AzureRmResourceGroup -Name myResourceGroup