Skip to main content

Developing on Azure with PowerShell

As we've all heard in our life at one point or another, "use the right tool for the job". When it comes to Azure, the right tool is PowerShell.

Today we're going to talk about how to connect to Azure with PowerShell and create a new VM.

1. An Azure account
2. A Resource Group within that Azure account
3. A Virtual Network and Subnet within that Azure account
4. A Security Group within that Azure account
5. Some former PowerShell knowledge
6. Admin rights on your machine.

The first thing we want to do is create a new connection between PowerShell on your machine and Azure. For this blog post, I'll be using Windows 10.

Let's go ahead and open up PowerShell as an administrator. Once you do that, we'll go ahead and run

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

We're setting it to "unrestricted" for the purposes of development and testing. In a production environment, it's best to use RemoteSigned to the scripts you run are signed by a specific party, or you know they were created for yourself.

After that, let's connect to Azure. For this, you'll need an Azure subscription or a free trial. To sign up for a free trial, please follow this link for directions:

Once you do that, we can go ahead and run Connect-AzAccount. This will connect us with our Microsoft account from PowerShell to Azure so we have the permissions to interact with Azure from PowerShell.

Let's run a quick az module cmdlet to make sure we can successfully interact with Azure. Please run

Get-AzVM | Select-Object Name,Location

You may not see anything here if you don't have any instances created in Azure. Regardless, as long as you don't get an error, you'll be good to go.

The next thing we want to do is create a quick PowerShell script that will allow us to create a new instance. I'm using VSCode with the PowerShell extension for this purpose. You can also use ISE, or any other editor you prefer.

Let's name our file New-AzureVM.ps1

First thing we'll do in our script is set up our function and our parameters. A quick rundown
1. [cmdletbinding] means we're writing an "advanced function". It allows our script to operate like compiled cmdlets in C#. You'll see confirmimpact and suportsshouldprocess args. confirmimpact tells PowerShell that this isn't a high task and to not prompt us for a yes or no. supportsshouldprocess opens up the ability to use $pscmdlet class, and in our case, we'll use an if statement that'll essentially say "if you see a certain parameter, continue running. If not, don't run the script".

2. The parameters that you see are the essentials for creating a VM. As you can see, some are mandatory and some are not. We're defining the East US region as the primary.

The next thing we'll do is add our begin block. This will allow optional, one-time processing of a specific action. In our case, we're confirming that the VM you're requesting to make is not already created. If it is, the script exits.

Now we'll move on to our process block. Our process block will have our "execution work" inside of it. It can be run any number of times, or none at all. As you can see, we also have a try/catch. This is our error handling. Another thing to point out is we're taking advantage of the $pscmdlet class again. The ThrowTerminatingError method allows us to push out whatever error message our function throws (if there is one) to the screen. The"$_" is a variable that essentially says "take everything and collect it here for use later". In our case, we're taking whatever error there is and storing it in that variable.

Finally, we'll just have our end block. The end block is typically used to either write a message saying that the function has completed, or closing connections.

Now, let's go ahead and run our new function/script. Save it anywhere, like on your desktop. Then open up a PowerShell ISE window as an administrator and navigate over to your desktop. Open up the function/script in ISE.

Click the green play button. This will load our script into memory vs having to create a module out of our script and importing it.

I have set up my function, wrote my cmdlet name, and put values in for my parameters. Let's kick it off!

If you stuck to the default image like I did (Windows Server 2016 Datacenter), you will get a UAC prompt. This UAC prompt is for the local admin user that you'd like to use for your instance.

If all went well, you will see a similar output to as the screenshot below:

And there you have it! You have automated your way into Azure and saved your poor mouse a TON of left clicks.

Code Resource:


Popular posts from this blog

Run PowerShell code with Ansible on a Windows Host

Ansible is one of the Configuration Manager kings in the game. With it's easy-to-understand syntax and even easier to use modules, Ansible is certainly a go-to when you're picking what Configuration Management you want to use for your organization. Your question may be "but Ansible is typically on Linux and what happens when I'm in a Windows environment?". Luckily I'm here to tell you that Ansible will still work! I was pleasantly surprised with how easy it is to use Ansible on Windows with a little WinRM magic. Let's get started.

Pre-requisites for this post:
1) WinRM set up to connect to your Windows host from Ansible
2) Ansible set up for Windows Remote Management
3) SSH access to the Ansible host
4) Proper firewall rules to allow WinRM (port 5985) access from your Ansible host to your Windows host
5) Hosts file set up in Ansible that has your IP or hostname of your Windows Server.
6) At least one Linux host running Ansible and one Windows Server host …

Running PowerShell commands in a Dockerfile

As Docker continues to grow we are starting to see the containerization engine more and more on Windows. With the need for containers on Windows, we also need the same automation we get in Linux with Dockerfiles. Today we're going to create a Dockerfile that runs PowerShell cmdlets.
Prerequisites; 1. Docker for Windows
2. A code editor (VSCode preferred)

Let's go ahead and get our Dockerfile set up. Below is the Dockerfile I used for this post.

from MAINTAINER Michael Levan RUN powershell -Command Install-WindowsFeature -Name Web-Server RUN powershell -Command New-Item -Type File -Path C:\ -Name config
As you can see from the above, this is a tiny Dockerfile. What this will do is install the IIS Windows 

Feature and create a new file in C:\ called "config".
You should see something very similar to the below screenshot;

Next let's create a running container out of our image. First we'll need to run docker container ls to

 get o…

DevOps tooling in the Microsoft realm

When I really started to dive into automation and practicing DevOps with specific tooling, there were a few key players. At the time Microsoft was not one of them. They were just starting to embrace the open source world, including the art and practice of DevOps. Since then Microsoft has went all in and the tech giant has made some incredible tooling. Recently I switched to a Microsoft-heavy environment and I love it. I went from AWS/Python/Ansible/Jenkins to Azure/PowerShell/ARM/Azure DevOps. My first programming language was PowerShell so being back in the saddle allowed me to do a full circle between all of the different types of tooling in both worlds. Today I want to share some of that tooling with you.

The first thing I want to talk about is ARM. What is ARM? ARM is a configuration management tool that allows you to perform software-defined-infrastructure. Much like Ansible and Terraform, ARM allows you to define what you want your environment to look like at scale. With ARM, yo…