다음을 통해 공유


Unattended Provisioning of Azure Virtual Network and Azure Virtual Machine using PowerShell

**
**

Introduction

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.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image001.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image002.png

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

 

Get-Module -ListAvailable Azure*

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image003.png

Connect to Azure

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

 

Login-AzureRmAccount

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image004.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image005.png

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.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image006.png

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

Prerequisites

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image007.png

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".

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image008.png

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"

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image009.png

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

 

Get-AzureRmStorageAccountNameAvailability "2017storageaccount"

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image010.png

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

 

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image011.png

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 10.0.0.0/16

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image012.png

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 10.0.0.0/16 -Subnet $VirtualSubnet

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image013.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image014.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image015.png

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.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image016.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image017.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image018.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image019.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image020.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image021.png

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image022.png

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.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/unattended-provisioning-of-azure-virtual-network-and-sharepoint-2016-virtual-mac/Images/image023.png

Summary

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