10 of the Best PowerShell Cmdlets for Microsoft Azure


Thomas Lee

By Thomas Lee, ImageFrame (v-tholee@microsoft.com)

Overview

PowerShell is Microsoft’s strategic task automation platform for IT Pros and enables you to automate your IT infrastructure. Azure is Microsoft’s cloud offering that provides IAAS and PAAS facilities (plus Azure is the base on which Office 365 is built). Azure (and Office 365) have a number of modules that provide cmdlets support. The core module for managing Azure is the Azure module which you can download from GitHub here.

The Azure Module(s)

The Azure module is, at present, a bit schizophrenic. This is a reflection of the fact that there are actually two main APIs for Azure: Azure Service Management and Azure Resource Management (aka ARM).

To support these two API sets there are two modules (Azure and AzureResourceManager); if you load the Azure module, you get access to the Service Management Cmdlets. You can then use the Switch-AzureMode cmdlets to access the Resource Management cmdlets. The latest module of the Azure module is version 0.9.5 (released on 17 July!). This contains 737 Service Management based cmdlets and 632 Resource Management cmdlets. The July release of the Azure module added 154 new cmdlets.

In the future, Microsoft will separate the modules into two, and will enable you to use the APIs side by side, without needing the Switch-AzureMode cmdlets. This will happen later this year, and I’ll be blogging more about it as things move forward.

So with a huge number of cmdlets, picking a top 10 is hard – as this represents a mere 0.73%of the total cmdlets available today. My top 10 list, therefore, is highly subjective and based on the cmdlets I use a lot to manage Azure Virtual Machines.

1. Add-AzureAccount

When you use the Azure cmdlets, the underlying APIs need some method of authentication (i.e. authenticating you to Azure). There are a couple of ways to do this, but I find the simplest is to use Add-AzureAccount. This cmdlet brings up a GUI that enables you to provide your credentials and once these are accepted, the cmdlets places a short-lived token that other cmdlets can use to prove you have been authenticated. I use this every day as the token life is just 12 hours.

2. Get-AzureVmImage

When you create a VM in Azure, you base the VM on an image – these can be Azure provided or provided by yourself. The image is, in effect, a syspreped version of the OS (similar to what comes on the Windows Server CD). Images come from Microsoft, 3rdparties – and you can convert any existing VM into an image to clone more VMs from a base image.

3. New AzureVmConfig

When you create a VM in Azure, you first create a VM configuration object using New-AzureVmConfig. Once the VM Configuration object is complete – you can create a VM using the New-AzureVM cmdlet! The New-AzureVm starts the VM using the configured parameters.

4. Add-AzureProvisioningConfig

After you create a VM configuration object, you add provisioning information to the configuration object using this cmdlet. You specify the image type (Windows or Linux) and the username and password for the initial admin user.

5. New-AzureVm

This cmdlet takes the VM Configuration object, modified by (for example) Add-AzureProvisioningConfig, and creates a VM. When you create an Azure VM, you can run it either inside a virtual network or inside an Azure Service. The service acts like a simple fail over load balancer – you first connect to the service, and the service connects to the VM. By default, Windows VMs get an RDP and a Remote PowerShell endpoint, while Linux VMs get an SSH endpoint.

6. Add-AzureEndpoint

In order that you can communicate with a Virtual Machine, you create endpoints – these enable the VM to receive the communication on a port by port basis. As I noted above, a few endpoints come to each VM but you can add more – for example, you could add Endpoints that pass port 80 and 443 traffic to the VM. You use the Add-AzureEndpointto create a new endpoint.

7. Update-AzureVM

With Azure VMs, you can use cmdlets, like Add-AzureEndpoints to change the configuration of a VM. To ensure you persist changes in the VM, you use Update-AzureVM to write the changes you just made (e.g. using Add-AzureEndpoint) back to Azure.

8. Get-AzureWinRMUri

Once you create a VM, by default you can access it via PowerShell remoting. In order to do this, you need the remote machine’s URI, which you can get using Get-AzureWinRMUri.

9. Get-AzureVM, Stop-AzureVM and Start-AzureVM

Once an Azure VM is created, you can get the VMs you have created (Using Get-AzureVM) as well as stop and start the VM. There are a number of reasons why stopping a VM makes sense – the most obvious being that you only pay for VM time when the VM is running. My test VMs are turned off at midnight most nights, and not turned on till 8:00 the next morning – this cuts 1/3 off my Azure compute charges. One thing to note: you must use an elevated PowerShell console (i.e. run PowerShell or PowerShell ISE as Administrator). If you don’t – Get-AzureVM returns nothing– not even an error message. And yes, I know these are actually 3 cmdlets, but they all use the same noun to I count them as one for the purposes of this list!

10. Set-AzureVMSize

When you create a new VM, it runs inside a specific VM instance. The VMSize specifies inter alia, how much RAM and how many logical processors your VM gets. Should you decide to change the instance size (i.e. add more ram and cores), you use the Set-AzureVMSize cmdlet, piping the output to Update-AzureVm.

You can see all 10 of these useful PowerShell cmdlets in my Azure Helper Module on GitHub. You can see these cmdlets being used in the New-tflWindowsVM, Net-tflLinuxVM and Set-tflAzurVMSizefunctions.

Summary

This has been a very quick look at 10 of the more useful Azure VM related service management oriented cmdlets. The list above explains what these cmdlets do – for more detail on how, there is a wealth of information on the web, including on my blog here.

My primary call to action is to get better with PowerShell: if you are not using it, start. If you are using it, learn more. And if you are good, why not come to the PowerShell community on Spiceworks. Join our community of IT Pros, and share your expertise!

Comments (0)

Skip to main content