When creating a VM, several options are available to you—including OS image, network configuration, and administrative credentials. However, a VM can also be created with default settings, using a minimal configuration.
The process of provisioning a VM consists of two different PowerShell commands. The first command, called Get-Credential, allows you to specify local administrator credentials for the VM. Once those credentials have been established, you can run the New-AzureRmVm command to configure and deploy a VM.
To create a local admin account and password for the VM being deployed, run the following command:
$cred = Get-Credential
This command results in a prompt, to which you can respond, by supplying a local admin account login and associated password. The login information provided in the prompt is then used by the VM deployment process to provision a local admin for the VM when it is deployed:
Running the $cred = Get-Credential command stores the local admin credentials that you provide in the $cred variable. After creating the local admin credentials, you can run the New-AzureRmVm command to provision the VM.
The $cred variable is referenced during the provisioning process, so that when the VM is provisioned, the local admin info that was provided is used.
To provision a new VM, run the entire following command in a PowerShell session:
New-AzureRmVm -ResourceGroupName "VMLab" `
-Name "myVM" `
-Location "EastUS" `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-SecurityGroupName "myNSG" `
-PublicIpAddressName "myPublicIP" `
-Credential $cred
The preceding command is a single-line command. Although the command itself is broken up into multiple lines, this command is one long command when written out.
To break the command up into multiple lines so that I can better explain it, I used the tick symbol ( ` ) to let PowerShell know it's one long command, despite being supplied over multiple lines.
If you prefer the copy/paste approach, visit mybook.thomasmitchell.net for an online version of all the commands in this book:
The tick symbol tells PowerShell that even though I'm supplying a multi-line command, it should be interpreted as a single-line command instead.
With that said, let's go through the command, line by line.
As you can see from the preceding command (which takes about 10-15 minutes to run), New-AzurermVM requires some basic information to provision a VM.
The ResourceGroupName switch tells the command which resource group the VM should be deployed to. In this case, the VM is going to be deployed into the VMLab resource group that was provisioned earlier (if you are following along with these instructions). The Name switch specifies the name of the VM being deployed. In this example, the VM is going to be called myVM.
The new VM is deployed into the EastUS region by specifying the Location switch.
Since the VM needs to go onto a network and subnet, this command includes the VirtualNetworkName switch and SubnetName switch. In the preceding example, the VM is being deployed to a virtual network called myVNet and a subnet called mySubnet. Because we haven't created a virtual network yet, nor a subnet, the New-AzureRmVM command is going to create them automatically, using default IP range values. Had we previously provisioned a virtual network and virtual subnet, we could specify their names instead, and the VM would be deployed to them.
To protect the VM, a network security group needs to be deployed. To provision and associate a network security group with the new VM, the SecurityGroupName switch is used. In the preceding example, the network security group is called myNSG. As was the case with the virtual network and virtual subnet, had we pre-provisioned another security group, we could have specified that group with the SecurityGroupName switch.
To be able to RDP to the new VM over the internet from a workstation, we need to give the VM a public IP address. This is done with the PublicIPAddressName switch. In this example, the public IP address resource is called myPublicIP.
In a production environment, assigning a public IP address and enabling RDP for a VM is a terrible practice. You never want to make RDP available over the internet on a production machine. I’m simply enabling RDP in this case for ease of use, so I can more easily work through the deployment process.
With that PSA out of the way, we must tell the New-AzureRmVm command what local admin credentials to provision for this VM. To do this, I'm specifying the Credential switch and referencing the $cred variable with it.
With all this information provided, running the New-AzureRmVm command will deploy a default Windows 2016 Server into the EastUS region, onto a new subnet called mySubnet, which is part of a virtual network called myVnet. The VM will be protected by a default set of rules contained in a network security group called MyNSG.
The VM deployed with the preceding command is called myVM and it will be deployed into a resource group called VMLab. It will assign a dynamic public IP address that is accessible from the internet and the local admin account, which will match what was configured when we ran the Get-Credential command:
The VM deployment process can take several minutes to complete, but when it does, you will have a fully functioning virtual machine deployed.
Once the deployment is complete, you can confirm in the Azure dashboard that the VM is up and running. You can also run the following command instead:
Get-AzureRmVm -resourcegroup VMLab -name MyVM -status
The output of the preceding command will show the status of your newly deployed VM.