Automate Azure VM deployment

  • Splatting parameters
  • First steps
  • Parameters
  • Confirm parameters
  • Conditional parameters
  • Creating and using objects
  • Full script

It’s been a while that I don’t bring something new to this blog, this is because I made a big change recently by moving from Brazil to Germany, I came to live here with my wife (which is Brazilian and German) so the adaptation and bureaucracies process takes much time, but now things are almost done and I hope to bring new content here more often 🙂

  • Splatting parameters

Splatting is a method which allows you to pass a collection of parameter values to a Powershell cmdlet as a unit, this makes your script a lot cleaner and easier to build and edit if needed.

To make use of this technique you need to create either an array or a hashtable to store the relation of “parameter + value”. I often use hashtables for it, after setting up the parameters and it’s values, you can finally splatt it by calling the hashtable variable with the at symbol ‘@‘ instead of a dollar sign ‘$‘.

The parameter name must be a valid parameter name of the cmdlet you’ll use, for the switch type parameters (present or not present) you set it’s value as $true, otherwise you can just ignore it.

#Using a cmdlet without splatting.
Cmdlet -Parameter1 'Value1' -Parameter2 'Value2' -Parameter3

#Using a cmdlet with splatting.
$Cmdlet_Params = @{
'Parameter1' = 'Value1'
'Parameter2' = 'Value2'
'Parameter3' = $true
Cmdlet @Cmdlet_Params

  • First steps

So, basically we’ll make use of Splatting method in this whole script, I chose to build this script in that way because we need to set a lot of parameters of each cmdlet used in it.

First, if you’ve never used Powershell-Azure before, there are a few commands you must run before:

Register-AzureRmResourceProvider -ProviderNamespace 'Microsoft.Network'
Register-AzureRmResourceProvider -ProviderNamespace 'Microsoft.Compute'
Register-AzureRmResourceProvider -ProviderNamespace 'Microsoft.Storage'

  • Parameters

As a best practice, we’d better to create a function and set the necessary parameters to it, this will make the script re-usable to create different VM’s with this same script.

  • Confirm parameters

I highly recommend you to always use a confirmation step in scripts like this one, due to it’s many parameters and what the script will achieve, if something goes wrong you’ll probably have some headache.

  • Conditional parameters

Note that we have two conditional parameters in our script, which are ‘New_VNET‘ and ‘NewResourceGroup‘, both only accepts the values of ‘yes‘ or ‘no‘.

If you intend to deploy the VM into an existing resource group and VNET in your azure account, you can just set ‘no‘ for both and typing their names for ‘ResourceGroupName‘ and ‘VnetName‘ parameters, just be sure to type their names correctly (use the confirmation step for it 😉 ).

Since it’s a conditional, we need to use the ‘if‘ statement in case we want to create a new resource group and VNET, note that you’ll need to provide the VNET and subnet addresses to proceed. 

  • Creating and using objects 

There are some steps where you can see that I create objects to store a result for using them later in another command like I did for Network Group Security rules.

See the lines 13, 27 and 34 in the script below

  • Full script

All other next steps follow the same basis:

  • Define parameters values into a hashtable
  • Splatting

Once you understand the concept and the syntax presented here, you can edit this script or create your own, you just know the information you need to set the parameters, simple like that 🙂

  • Conclusion

This is my first Azure automation article, I hope you liked it. I covered the explanations focused in the script making and not so much in the Azure VM creation itself, by assuming you already know a little about it by doing manually in Azure portal.

If you need me to explain more about the Azure VM creation and deployment, please don’t hesitate to contact me or comment it here, I’d be glad to bring another article to cover this subject!

Thank you for reading!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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