DEV Community

Cover image for Deploy a Virtual Machine using Azure PowerShell
Lakshit Pant
Lakshit Pant

Posted on

Deploy a Virtual Machine using Azure PowerShell

Today is Day 6 of my 100 Days of Cloud challenge, and I'm going to repeat what I did yesterday, but with a twist.

On Day 5, I put a Virtual Machine into Azure using the Portal and explained each step. Today, I'm going to use PowerShell to do the same thing. This means that instead of clicking around a lot (and making you scroll endlessly, dear reader), I can now accomplish the task with just one command.

Azure Powershell

I'm using portal.azure.com powershell here, as if any user is working on MAC, this would help everyone feels same. Doing this helps us to use some less commands, like you don't have to write a command for Azure Connection. So, let's go with the show on the road.

get-help New-AzVM
Enter fullscreen mode Exit fullscreen mode

Commands here

Certainly! Breaking it down:

Wow, that's a ton of information, and when it all shows up on the screen like that, it can be overwhelming. However, let's focus on some important details.

Firstly, check out the "SYNTAX" section. Here, you'll see a list of parameters that you can use with "New-AzVM." These parameters should look familiar because they're the same things I used when creating the VM in the Portal.

You've already created a Resource Group using PowerShell on Day 3, and you know its name. Also, you've identified that your location is "eastus" as indicated in the output of "Get-AzVM."

Given this, the PowerShell command for this part would be:

Powershell location cmd

Secondly, if we check the "REMARKS" section, there are additional commands we can run, and one of them provides examples. Let's run that command and see what it gives us.

Example 1

Example 2 :

Example 2

Now, there are typically two ways by which you can make a VM here, but first you've set the credentials first, there is one method by using -Verbose as a parameter that allows you to create a VM with default configurations.

You must have noticed about several examples, and the first one seems surprisingly straightforward, just asking for a Name parameter for the VM and my credentials. It can't be that easy, can it? Let's give it a shot— I'll run this with a "-Verbose" switch to get some output.

Credentials

But as soon you run it, you'll see this:

Image description

Run these commands to set this up :

$securePassword = ConvertTo-SecureString 'Changethepasswordhere' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('Changetheuserhere', $securePassword)
Enter fullscreen mode Exit fullscreen mode

Setup Credentials

Here we go with the results:

User Password is set now

Let's deploy the VM now:

Either you copy the command or you know your VM type you want to deploy alike those examples we saw earlier you can always edit those. Or for learning purpose, you can use the default VM Machine, use the -Verbose parameter.

New-AzVM -Name MYPSTestVM -Credential (Get-Credential) -Verbose
Enter fullscreen mode Exit fullscreen mode

Verbose utilization

This is how you do it, now wait for this to finish & all set.
VM all set

Get-AzVM
Enter fullscreen mode Exit fullscreen mode

Image description

And check the Portal, there’s the new Resource Group:

Resource Group

And if I click into that, there’s all my resources:

Image description

This is why understanding the parameters is crucial; it ensures that the Virtual Machine we create is configured the way we want it. Checking the machine in the Azure Pricing Calculator, including a Windows License and SSD. While Microsoft provides defaults, for testing purposes, I initially considered deleting it.

Moving forward, I want to create a new VM in the correct Resource Group with specific options. Looking at the "-Verbose" output from creating the first VM guides me on the parameters and options I need. I want to specify the following:

  • Resource Group: MyExamplePowerShellRG2
  • Location: eastus
  • Name: MyPowerShellVM (VM name in the portal, not the local computer name)
  • AddressPrefix: "10.30.0.0/16" (for the Virtual network)
  • SubnetName: PSVMSubnet
  • SubnetAddressPrefix: "10.30.30.0/24"
  • PublicIPAddressName: PSPublicIP
  • DomainNameLabel: psvm001md (local)
  • SecurityGroupName: PSVMNSG
  • OpenPorts: 3389 (for RDP Connectivity, or SSH for Linux)
  • ImageName: "Win2016DataCenter"
  • Size: "Standard_B2s" (VM Size; a full list can be found here)
  • OSDiskDeleteOption: Delete (specifies whether the OS Disk is deleted or detached and retained when the VM is deleted; options are Delete, Detach)

Referring to "get-help" for the command or the official Microsoft Docs article for "New-AzVM" reveals that these are just a few options available, but they are likely the most common ones. With these options, my PowerShell command should look like this:

New-AzVM -ResourceGroupName MyExamplePowerShellRG2 -Location eastus -Name MyPowerShellVM -AddressPrefix "10.30.0.0/16" -SubnetName PSVMSubnet -SubnetAddressPrefix "10.30.30.0/24" -PublicIPAddressName PSPublicIP -DomainNameLabel psvm001md -SecurityGroupName PSVMNSG -OpenPorts 3389 -ImageName "Win2016DataCenter" -Size "Standard_B2s" -OSDiskDeleteOption Delete
Enter fullscreen mode Exit fullscreen mode

Now, let’s delete the VM. I’ll run

get-help Remove-AzVM

Enter fullscreen mode Exit fullscreen mode

*Removing Resource Group: *

Remove-AzVM -ResourceGroupName MyExamplePowerShellRG2 -Name MyPowerShellVM

Enter fullscreen mode Exit fullscreen mode

And all set, now you're Scripting dude, writing your first Powershell script today for making these Virtual Machines.

See you tomorrow with Azure CLI.

Top comments (0)