It is no surprise that we commonly face repetitive and time-consuming tasks. For example, you might want to create multiple storage accounts. You would have to follow the same steps multiple times to get your job done. This is why Microsoft supports its Azure services with multiple ways of automating most of the tasks that can be implemented in Azure. In this Azure Powershell tutorial, we will learn how to automate redundant tasks on Azure cloud.
This article is an excerpt from the book, Hands-On Networking with Azure, written by Mohamed Waly.
PowerShell is commonly used with most Microsoft products, and Azure is no less important than any of these products.
You can use Azure PowerShell cmdlets to manage Azure Networking tasks, however, you should be aware that Microsoft Azure has two types of cmdlets, one for the ASM model, and another for the ARM model.
The main difference between cmdlets of the ASM model and the ARM model is, there will be an RM added to the cmdlet of the current portal.
For example, if you want to create an ASM virtual network, you would use the following cmdlet:
New-AzureVirtualNetwork
But for the ARM model, you would use the following:
New-AzureRMVirtualNetwork
By default, you can use Azure PowerShell cmdlets in Windows PowerShell, but you will have to install its module first.
There are two ways of installing the Azure PowerShell module on Windows:
The following are the required steps to get Azure PowerShell installed:
To be able to run your PowerShell cmdlets against Azure successfully, you need to log in first to Azure using the following cmdlet:
Login-AzureRMAccount
Then, you will be prompted to enter the credentials of your Azure account. Voila! You are logged in and you can run Azure PowerShell cmdlets successfully.
To create an Azure VNet, you first need to create the subnets that will be attached to this virtual network. Therefore, let's get started by creating the subnets:
$NSubnet = New-AzureRMVirtualNetworkSubnetConfig –Name NSubnet -AddressPrefix 192.168.1.0/24 $GWSubnet = New-AzureRMVirtualNetworkSubnetConfig –Name GatewaySubnet -AddressPrefix 192.168.2.0/27
Now you are ready to create a virtual network by triggering the following cmdlet:
New-AzureRMVirtualNetwork -ResourceGroupName PacktPub -Location WestEurope -Name PSVNet -AddressPrefix 192.168.0.0/16 -Subnet $NSubnet,$GWSubnet
Congratulations! You have your virtual network up and running with two subnets associated to it, one of them is a gateway subnet.
To add an address space to a virtual network, you need to retrieve the virtual network first and store it in a variable by running the following cmdlet:
$VNet = Get-AzureRMVirtualNetwork -ResourceGroupName PacktPub -Name PSVNet
Then, you can add the address space by running the following cmdlet:
$VNet.AddressSpace.AddressPrefixes.Add("10.1.0.0/16")
Finally, you need to save the changes you have made by running the following cmdlet:
Set-AzureRmVirtualNetwork -VirtualNetwork $VNet
Azure CLI is an open source, cross-platform that supports implementing all the tasks you can do in Azure portal, with commands.
Azure CLI comes in two flavors:
Throughout this book, we will be using Azure CLI 2.0, so let's get started with its installation.
Perform the following steps to install Azure CLI 2.0:
To create a virtual network using Azure CLI 2.0, you have to follow these steps:
az network vnet create --name CLIVNet --resource-group PacktPub --location westeurope --address-prefix 192.168.0.0/16 --subnet-name s1 --subnet-prefix 192.168.1.0/24
To add a gateway subnet to a virtual network, you need to run the following command:
az network vnet subnet create --address-prefix 192.168.7.0/27 --name GatewaySubnet --resource-group PacktPub --vnet-name CLIVNet
To add an address space to a virtual network, you can run the following command:
az network vnet update address-prefixes –add <Add JSON String>
Remember that you will need to add a JSON string that describes the address space.
To summarize, we learned how to automate cloud tasks using PowerShell and Azure CLI. Check out the book Hands-On Networking with Azure, to learn how to build large-scale, real-world apps using Azure networking solutions.
Creating Multitenant Applications in Azure
Fine Tune Your Web Application by Profiling and Automation
Putting Your Database at the Heart of Azure Solutions