Friday, May 15, 2020

Essentials to Create a Virtual Machine using PowerShell on Azure

Introduction

 
There are various options/parameters which would be used while creating a Virtual Machine either by using Azure Portal or by using Powershell AZ module
 
Prerequisites:
  • Powershell 6+
  • Az Module
In this section, we’ll discuss the following parameters:
  1. Resource Group Name
    It is a kind of virtual container. Each resource in Azure should be assigned to a Resource Group

  2. Virtual Machine Name
    That is the user’s choice. You can keep any name for your virtual machine.

  3. Location
    In which location do you want to spin-up your virtual machine? Azure has various regions across the globe. It is advised to read about latency and available services in different Azure regions beforehand.

  4. Virtual Network Name
    You can either create a new virtual network or assign the existing one to your virtual machine

  5. Subnet Name
    The subnet is an integral part of a virtual network. You can either create a new subnet or assign the existing subnet to your virtual machine

  6. Security Group Name
    The Network Security Group is also known as Firewall. It protects from direct access to your virtual machine. It could be directly assigned to a virtual machine or the subnet. That is a design choice.

  7. Public IP Address Name
    It is the address through which this machine could be accessed

  8. Open Ports
    If you want various options to allow your virtual machine to be accessed like HTTP, HTTPS, RDP, etc.

    Port 80 is for HTTP, Port 443 is for https (secure connection), Port 3389 for RDP access.

Create a VM using Powershell

 
Ensure you have 6.0.0 or above Powershell version installed and the AZ module installed before running the command on Powershell:
 
C:\> New-AzVM -ResourceGroupName “azrgdemo” -Name “azvmdemomachine” -Location “EastUS” -VirtualNetworkName “azvnetdemo” -SubnetName “default” -SecurityGroupName “azdemoNSG” -PublicIpAddressName “azdemoipaddr” -OpenPorts 80,3389,443
 
Though there are a couple of other optional parameters that you could also pass in the command, such as Tags, however, I have skipped this for this moment.
 
When you run the above command, the Powershell will ask for the credentials
 
Example
 
User: abhishekmaitrey
Password for user abhishekmaitrey: **********
 
Now the Powershell AZ module will execute this command and start the VM creation process. This activity may take some time. Once the command has executed successfully, you will be notified with some detailed information on a new virtual machine. You should see the following:
  • Virtual Machine Id – It is generally a hashed value like GUID
  • Fully Qualified Domain Name

No comments:

Post a Comment