Powered by Microsoft Azure
https://www.facebook.com/troubleshootingazure

Azure & PowerShell – Writing Scripts !!

azure, cybersecurity, IT

Azure & PowerShell – Writing Scripts !!

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

2 Responses

  1. zoritoler imol says:

    Normally I do not read post on blogs, but I wish to say that this write-up very forced me to try and do it! Your writing taste has been amazed me. Thank you, very great article.

  2. Faviola Waltjen says:

    I think this is a real good article.Really thank you! truly Great.

Leave a Reply

Your email address will not be published.