Azure & PowerShell Custom Code

I have been asked a few times about the easiest way to write a PowerShell script or how to get started writing a script and automating stuff in Azure. Or writing a complex script to automate a complex deployment.

(PS – This blog is just to help you expand you PS skills, and does not help in anyway if you’re a beginner)

The simple answer is Small Steps!! And Practice.

So, let us take an example, you have the following requirement and want to automate it in PowerShell:

Deploy a Virtual Network and three subnets in it.  Each subnet should have a network security group attached to it and the NSG should have allowed TCP inbound/outbound to rule attached. Subnet 1 should have a VM with public IP Address and all the traffic should be routed via a virtual machine. Here VM can be considered as a firewall that all traffic is always routed via the VM.

This is an easy task if you break this up into pieces (aka Small Steps).

Let’s break it apart and see what’s needed:

  • A resource Group
  • One Virtual Network
  • Three Subnets
  • Network Security Group
  • Inbound and Outbound Rules
  • Public IP
  • A virtual machine
  • Route Table.
  • Routing Rules

Finding commands using PowerShell ISE:

  1. Open PowerShell ISE and open the command ad-on, which open up on the right side:

2. In the name window, start with the resource you need to find. For example, creating a new network would be “New-Az..” followed by the resource name.

3. Select the command and click on Show Details. This should open up a window below and the properties marked with asterisks (*) are mandatory. Once all details are populated, copy and paste it to scripting areas as shown:

4. Do it for all the resources. And this is what it should look like:

Here are all the PS commands:

  • A resource Group: New-AzResourceGroup
  • One Virtual Network: New-AzVirtualNetworkSubnetConfig
  • Three Subnets: New-AzVirtualNetworkSubnetConfig
  • Network Security Group: New-AzNetworkSecurityGroup
  • Inbound and Outbound Rules: New-AzNetworkSecurityRuleConfig
  • Public IP: New-AzPublicIpAddress
  • A virtual Machine: New-AzVM
  • Route Table: New-AzRouteTable
  • Routing Rules: New-AzRouteConfig

5. Adding variables:

You can store all types of values in PowerShell variables. For example, store the results of commands, and store elements that are used in commands and expressions, such as names, paths, settings, and values.

A variable is a unit of memory in which values are stored. In PowerShell, variables are represented by text strings that begin with a dollar sign ($), such as $a, $process, or $my_var.

Variable names aren’t case-sensitive, and can include spaces and special characters. But, variable names that include special characters and spaces are difficult to use and should be avoided.

Remove all the static values and use variables instead.

6. Using Loops and Import data:

Loops can be used to execute the same function multiple times.

“For loop, For Each-Object loop, and the While, and Do-While”

For Loop:

For ($i=0; $i -le 10; $i++) {
    “10 * $i = ” + (10 * $i)

For Each Object Loop:

The For statement (also known as a For loop) is a language construct you can use to create a loop that runs commands in a command block while a specified condition evaluates to $true.

A typical use of the For loop is to iterate an array of values and to operate on a subset of these values. In most cases, if you want to iterate all the values in an array, consider using a Foreach statement.

Importing files or reading data from files can be handy and the easiest way is using Import-CSV Command. Refer this below URL:

Import-Csv (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Docs

So, coming on to the example above – we need to create multiple subnets under the same vNET. This uses the same function/command.

The first step is to create a CSV file, similar to this:

And then import the file after which read one line at a time using a for loop.

The steps would now look like this:

  • Create a vNET (Step 1 below)
  • Import the CSV file (Step 2 below)
  • Run a loop (for each loop) (Step 3 below)
  • Create and attach subnets to the vNET. (Step 4 below)

Starting with the commands for each task, then breaking up into variables should give a good start writing a script. The next step is finding the task which are repetitive – once the tasks are figured out, the next step is either creating an Array (Google “PoweShell Arrays”) or import data using a file. Once you have minimized the amount of data/lines of code – you should be good to go.

Small steps everytime is the key !!

In the main example – you can use loops for multiple NSGs, create NSG rules (Can use CSV files for rules!!) or in case you want to create VM’s, you can have VM config (OS, SKU, Disk, etc) in a CSV file and then deploy VM’s

Q: What is Azure?

A: Azure is a cloud computing platform provided by Microsoft that allows users to build, deploy, and manage applications and services through a global network of data centers.

Q: What is PowerShell?

A: PowerShell is a command-line shell and scripting language developed by Microsoft for automating tasks in Windows environments and managing Microsoft products such as Azure.

Q: How can I use Azure with PowerShell?

A: You can use Azure PowerShell cmdlets and scripts to automate tasks and manage resources in Azure, such as creating virtual machines, managing storage accounts, and deploying applications.

Q: How can I create a virtual machine using Azure PowerShell?

A: To create a virtual machine using Azure PowerShell, you can use the New-AzVM cmdlet and specify the virtual machine name, image, size, and other parameters such as the resource group name and location.

Q: What are some examples of tasks I can automate using Azure PowerShell?

A: You can automate tasks such as creating and managing virtual machines, managing storage accounts, deploying and managing applications, and managing Azure resources such as virtual networks and security groups.

Q: How can I install Azure PowerShell?

A: You can install Azure PowerShell from the PowerShell Gallery using the Install-Module cmdlet. You can also download and install the Azure PowerShell module from the Azure portal.

Q: How do I connect to my Azure account using PowerShell?

A: You can connect to your Azure account using the Connect-AzAccount cmdlet and providing your Azure credentials. You can also connect to your Azure account using the Azure CLI or the Azure portal.

Q: What is Azure Resource Manager?

A: Azure Resource Manager is a management framework that allows you to deploy, manage, and monitor resources in Azure, such as virtual machines, storage accounts, and SQL databases. You can use Azure Resource Manager templates to define and deploy your resources in a repeatable and consistent manner.

Q: What are some best practices for writing PowerShell scripts in Azure?

A: Some best practices for writing PowerShell scripts in Azure include using descriptive variable names, commenting your code, using error handling and logging, and testing your scripts thoroughly before deploying them.

Q: How can I run PowerShell scripts in Azure?

A: You can run PowerShell scripts in Azure using Azure Automation, Azure Functions, or by running the script directly on an Azure virtual machine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.