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

So, you want to be a Cloud Engineer?

In 2019 one of the biggest pieces of tech is the cloud. Whether it be public cloud or private cloud, cloud technologies are here to stay (for now). I predict that Cloud Engineering will be a very big part of IT (and development) for another 5-10 years. Today I want to share with you my journey in becoming a Cloud Engineer and some helpful tips. A career timeline to be a Cloud Engineer can go like so;

Desktop Support > Junior Sysadmin > Sysadmin > Sysadmin/Technical Lead > Engineer >  Cloud Engineer.

Although our career paths may not align, I believe that this progression is very import. Let me tell you why.

Helpdesk/Desktop Support Helpdesk and desktop support get your feet wet. It allows you to understand technology and how it's used in the workplace from a business perspective. It shows you what technologies may be best in the current environment your in and how to support those technologies. It also teaches you soft skills and how to support people from a technic…

Monitoring your containers in an AKS cluster with Prometheus

Monitoring and alerting is arguably one of the most important thing in Cloud Engineering and DevOps. It's the difference between your clients stack being up and a client being down. Most of us have SLA's to abide by (for good reason). Today we're going to learn how to spin up Prometheus in an AKS cluster to monitor our applications.

1. Intermediate knowledge of Kubernetes
2. An AKS cluster spun up in Azure

Recently AKS supports Prometheus via Helm, so we'll use that for an automated solution to spin this up. This installs kube-prometheus, which is a containerized version of the application. With raw Prometheus, there are a few things that are needed for the operator;

1. Prometheus: Defines a desired deployment.
2. ServiceMonitor: Specifies how groups of services should be monitored
3. Alertmanager: Defines the operator to ensure services and deployments are running by matching the resource

With kube-prometheus, it is all packaged for you. This means configuri…

Spinning up a Kubernetes cluster with Kubeadm

In today's world, we have several public cloud technologies that will ultimately help us with spinning up these infrastructures. This however comes with a price. Because a public cloud provider (like AWS or Azure) handles the API/master server and networking, you'll get something up quick, but miss some key lessons of spinning up a Kubernetes cluster. Today, I'll help you with that.

There are some pre-reqs for this blog:
1. At least 3 VM's. In my case, I'm using my ESXi 6.7 server at home.
2. Basic knowledge/understanding of what Kubernetes is utilized for.
3. Windows, Mac, or Linux desktop. For this blog, I am using Windows 10.

The first thing you want to do is spin up three virtual machines running Ubuntu18.04. You can use a RHEL based system, but the commands I show and run (including the repos I'm using) will be different.

I have already set up my 3 virtual machines. I gave them static IP addresses as I have found API/configuration issues if the VM shuts do…