I was recently asked to quickly audit a customer’s environment for all running VMs. I quickly reached for my PowerShell toolbox and put together the following script.

In the example below I have used the table grid views available. It would be just as easy to push all this info to a csv file. For swiftness this was my approach, I’d be very interested to hear from the gurus out there to see what your preference is and how you would do this.

Remember there is always a better way, just don’t keep that to yourself!

I have a number of customers implementing HUB benefit for their IaaS VMs in Azure. In all cases to date this is a rebuild or new build as part of a migration. It works very well, if licensed, you should definitely be looking at this option to drive down costs.

This script created a new network but in most instances a network will already exist and although you will create a new NIC you will want to place this VM into an existing subnet.

The extract below can be used to create a new NIC but add this to a named vNet and Subnet.
When you define the VM configuration you would use this to be the NIC.
Finally make sure this (if the first NIC) is set as -Primary

Tagging in Azure is a massively useful feature. I have customers who are interested in identifying resources for billing but they are also a very useful tool for control. Resources can be grouped by tag and then a script can be used to apply a function to all machines or services with the same tag.

In the example below I call a variable that looks for Azure resources where the type is identified as a Microsoft virtual machine. Calling this function enables me to extract a range of information. (I fact this script then goes on and uses the ResourceId too)

As referenced in Using tags to organize your Azure resources tags are updated as a whole so if you want to add additional tags you first have to call the existing tags. In the example below I am adding the new tag to my existing tags.

Finally we are looping this for each vm and applying via a set command.

Working with Azure in the enterprise means you will quickly want to create your own custom images. In this introductory article I will show you an example of how to create an image from an existing generalized imaged.

Please note:

This is utilising the ARM model and does not apply to Classic.

This assumes you have created a generalized image in Azure and know where it is!

This process is not considering on premises VMs.

This process uses Windows images.

The following documents and articles were used to create the script below. Many thanks to the efforts and hard work of the authors.

With the release of the RunAs feature in Azure Automation. A service account can now be called in Azure Automation scripts to enable and run Resource Manager functions.

I think one of the most useful features of this is to auto shutdown virtual machines by TAG.

In the example below I have set up the following:

A TAG called “Environment” with and ID of “Lab” and applied to each VM I want to control.

A RunAs service account as part of my AzureAutomation resource.

A PowerShell Workflow script to scan for the TAGs applied to Windows virtual machines and to shut them all down in parallel.

An Azure Automation schedule to run Monday-Friday at 17:00 to call the published workflow.

When configuring a schedule through the browser it uses the browser’s local time zone. This is stored in UTC but is converted for you. You’ll need to consider this is managing multiple resources across the globe.

From a best practise point of view, I try to use Resource Groups as containers for all items I consider as part of the same lifecycle. This allows me to remove, delete and recreate what I need without fear of losing a component and generally allows me to be more efficient. I like peace of mind.

However, when it comes to best practise sometimes it feels slow and cumbersome. “This is the Cloud I don’t want to be held back by anything” I shout from the roof tops in my superhero pyjamas!

The truth is best practise and / or procedure shouldn’t get in the way of anything at all but if I’m just spinning up a few resources to test a lab someone has sent me on a payment gateway or vNet to vNet VPN set up, this pyjama wearing superhero isn’t waiting around for anyone. I therefore plough ahead.

For single resources I would first get the details of the resource you would like to move, this is important because you may have named two resources the same (remember what I was saying about being rash).

Please note: It will move dependencies, for example if you want to move a VM it will move the components such as Public IP and Network Security Group. As is, you will be prompted that you want to move the resource and the associated resources, if they exist.

Moving resources is simple and easy. Best practise is important and no Cloud architect should be seen in public in their superhero pyjamas!

Azure Commander over and out……

Disclaimer: Please note although I work for Microsoft the information provided here does not represent an official Microsoft position and is provided as is.

Last Tweets

I was lucky enough to join the Australian Institute of Company Directors swim team for the #PorttoPub swim in Perth Western Australia. The race was called off at the three hour mark due to the tough conditions. However it proved again to me that a good t…https://t.co/AMf3zGNVEx,7 hours ago