Unattended Provisioning Of Azure Virtual Network And SharePoint 2016 Virtual Machine Using PowerShell

It has been a great journey with C# Corner and it gives me immense pleasure to publish my 200th article. I would like to take this opportunity to thank the entire C# Corner team for their relentless support and give a big thank you to all the readers and for the criticism, which helped me improve and serve the community better.

Azure is a great offering from Microsoft, where the users can leverage it in many ways like Platform as a Service, Infrastructure as a Service (IaaS) and Software as a Service (Saas). We can spin up virtual machines in Azure in a few minutes by utilizing the existing images in the Azure Gallery. In this article, we will see how we can create a virtual network and provision a virtual machine within the created virtual network, using PowerShell on a remote basis from our local machine. In this way, we don’t have to login to Azure Management Portal and it provides an unattended way of virtual machine provisioning.

Azure PowerShell

In order to start working with Azure from PowerShell on a remote basis, we have to download and install Azure PowerShell module in our local machine. Azure PowerShell can be download from this link.

On successful installation, you will get the screen given below.

To test Azure PowerShell installation, run the command given below, which will list Azure modules available.

Get-Module -ListAvailable Azure*

Connect to Azure

Once the installation completes spin up Windows PowerShell, add the command given below to connect to Azure Subscription.


If successfully logged in, we will get the screen given below.

We will divide the article into two sections, which are given below.

  • Creation of Virtual Network
  • Provisioning of Virtual Machine

Creation of virtual network

Virtual network acts as a container for multiple virtual machines.

In order to set up the virtual network, we have to ensure that the prerequisites given below are in place.

  • Resource Group
  • Storage Account
  • Subnet for Virtual Network


Let’s configure the prerequisites first before setting up the virtual network.

Resource Group

Resource group acts as a container for all the resources. In order to create the resource group, we need to specify the location for the resources. We can get the available set of resources by running the script given below.

Get-AzureRmLocation | sort Location | Select Location

Now, let’s go ahead and create a resource group by the name ‘AzureResourceGroup’ in the location ‘southeastasia’. We will use the command given below to provision the resource group

New-AzureRmResourceGroup -Name "AzureResourceGroup" -Location "southeastasia".

Storage Account for Virtual Machine

Each VM requires a virtual hard disk. Hence, let’s create a storage account that will hold the virtual hard disk. You will have to use unique names for storage accounts. The account name has to be in lower case and between 3-24 characters in length. So let’s see the availability of the names using the command given below.

Get-AzureRmStorageAccountNameAvailability "storageaccount"

The storage account name given above is already taken. Thus, let’s try a new one.

Get-AzureRmStorageAccountNameAvailability "2017storageaccount"

The storage account is then crated, using the command given below.

$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName "AzureResourceGroup" -Name "2017storageaccount" -SkuName "Standard_LRS" -Kind "Storage" -Location "southeastasia"

Create a subnet

Subnet is a range of IP addresses in the virtual network. We can divide a virtual network into multiple subnets for organization and security. Now, let’s go ahead and create the final piece that has to be set up prior to the creation of the virtual network, which is subnet. We can create subnet using the command given below.

$VirtualSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name "2017Subnet" -AddressPrefix

Create the Virtual Network

Once the pieces given above are in place, we are good to go and create virtual network. Run the command given below with the previously setup parameters to create a virtual network by the name “2017VirtualNetwork”

$VirtualNetwork = New-AzureRmVirtualNetwork -Name "2017VirtualNetwork" -ResourceGroupName "AzureResourceGroup" -Location "southeastasia" -AddressPrefix -Subnet $VirtualSubnet

Once the command is run, it will create the virtual network, which we can test, as shown below.

Provision Virtual Machine

The virtual network is in place. Let’s start with the creation of a virtual machine.

Public IP and NIC

When we create Azure public-facing Services that will connect to the Internet; we need a Public IP and NIC. Once the Public IP has been created, in order to assign it to the virtual machine; we can assign the Public IP to the network interface (NIC) of the VM. The commands given below can be used to provision the Public IP as well the NIC. Later, we will be associating the NIC with VM during the creation time.

$PublicIP = New-AzureRmPublicIpAddress -Name "PublicIP" -ResourceGroupName "AzureResourceGroup" -Location "southeastasia" -AllocationMethod Dynamic

$NIC = New-AzureRmNetworkInterface -Name "NIC" -ResourceGroupName "AzureResourceGroup" -Location "southeastasia" -SubnetId $VirtualNetwork.Subnets[0].Id -PublicIpAddressId $PublicIP.Id

Provisioning Virtual Machine

In order to create the virtual machine, let’s get the administrative user name and password, which will be used with VM.

The creation of the virtual machine requires a configuration object, which will be used in VM Creation script. Let’s set up the configuration object first. Run the command given below to add VM Name and Size to the object. To get a fair idea about the available sizes, you can take a look here. Since SharePoint requires fairly good amount of resources, I am going with D3 size.

$SPVM = New-AzureRmVMConfig -VMName "SPVM2017" -VMSize "Standard_D3"

Now, update the configuration object with the computer name and credentials that will be used for administrative purposes (which we had earlier accepted from the user).

$SPVM = Set-AzureRmVMOperatingSystem -VM $SPVM -Windows -ComputerName "SPVM2017" -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

Now, we need to update the configuration object with the image, which will be used to create VM. Here, we will be using SharePoint 2016 image. The various available images and their information that we need to use in the command can be fetched from here.

$SPVM = Set-AzureRmVMSourceImage -VM $SPVM -PublisherName "MicrosoftSharePoint" -Offer "MicrosoftSharePointServer" -Skus "2016" -Version "latest"

Once the image has been added to the object, we will add NIC that contains the Public IP information to the object.

 $SPVM = Add-AzureRmVMNetworkInterface -VM $SPVM -Id $NIC.Id

Virtual machine will internally use a virtual hard disk. Let’s specify the container for the virtual hard disk by running the commands given below.

$VirtualDiskLoc = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/SPDisk.vhd"

$SPVM = Set-AzureRmVMOSDisk -VM $SPVM -Name "SPDisk" -VhdUri $VirtualDiskLoc -CreateOption fromImage

The configuration object has been updated with the required details. We are all set to start the creation of the virtual machine by running the command given below.

New-AzureRmVM -ResourceGroupName "AzureResourceGroup" -Location "southeastasia" -VM $SPVM

The virtual machine has been successfully created within the virtual network and we can see it by going to Azure Management portal.

Click on the newly created VM and connect to it via RDP.

Now, we have connected to the virtual machine and can see that SharePoint has been installed within the VM. Since we have just used SharePoint Image, only the SharePoint 2016 installation part will be done during the creation of the VM. To start using SharePoint Server 2016 in full swing, we will have to install SQL Server, run SharePoint products and configuration wizard to connect to SQL Server, which will create the configuration database for SharePoint.


Thus, we saw how to create a virtual network in SharePoint 2016 virtual machine in Azure, using PowerShell.