Wednesday, 6 May 2020

Create an Azure VM with Powershell

Hello,

Lets see the simplest way to create a Windows VM with Azure PowerShell.

There are some prerequisites before we start.

We have to have the following in place which provide the VM a purpose.

A valid subscription to Azure.
I am using the Native Cloud Shell in the Azure Portal with "Powershell"  as the CLI





A Resource Group

Which can be created with
New-AzResourceGroup -Name TestResourceGroup -Location WestUS



A Virtual Network and a Subnet to place your VM

$vNet = New-AzVirtualNetwork `
  -ResourceGroupName TestResourceGroup `
  -Location WestUS `
  -Name myVirtualNetwork `
  -AddressPrefix 10.0.0.0/16




 Define a Subnet and link to the Virtual Network Created.

$subnetConfig = Add-AzVirtualNetworkSubnetConfig `
  -Name ProdSubnet `
  -AddressPrefix 10.0.0.0/24 `
  -VirtualNetwork $vNet


 $vNet | Set-AzVirtualNetwork









A VM Size to choose from :
We are using "Standard_B1ls"

Deploy a Test VM.

 New-AzVM -Name appserver03 -ResourceGroupName "TestResourceGroup"`
 -Location "West US" `
 -VirtualNetworkName "myVirtualNetwork" `
 -SubnetName "ProdSubnet" `
 -Image Win2016Datacenter  -Size "Standard_B1ls"
 Enter the Credentials for the new VM. *It cannot be Administrator.
If you want to run the deploy as a Job use the parameter "-AsJob"



 This would complete the deployment.

Fetch the Public IP of the VM deployed to connect via RDP.

Get-AzPublicIpAddress `
  -Name appserver03 `
  -ResourceGroupName TestResourceGroup `
  | Select -expandproperty IpAddress

Happy Scripting.

No comments:

Post a Comment