Loading

TLDR: Needed a way to start a VM where an end user could do it, without the end user having to go through the portal or start a script…kind of.

To preface this, I came upon a little challenge, ok. I needed to create a solution where someone who has no technical expertise and had no business going around in Azure Portal needed to start a VM.

I’m thinking hm. Well, we could create the runbook first that would start your specific VM you want. Then create a webhook. Then, I was thinking, how would an end user interact with this webhook? How can they call or POST this webhook to start ‘their’ VM? PowerShell? Hmm maybe but the end-user would have to open it and then run it. Too complicated for the casual business user. Also, some shops have PowerShell locked down.

Ah, years ago I used a module called PS2EXE. Converts a ps1 file to an executable. That way a user can just double click on an ‘application’ on their desktop, and it uses the webhook to start an automation runbook which turns on my VM.

Here’s how I did it:

1.) Create a runbook to start your VM.

Something like this should be fine. Of course, you can always make it fancier and maybe you should, but for this case it would be this:

Disable-AzContextAutosave -Scope Process
Connect-AzAccount -Identity
$AzureContext = Set-AzContext -SubscriptionId "#########-######-######-############"

$StartVM = Start-AzVm -Name 'VM Name' -ResourceGroupName 'Name of Resource Group here'
$StartVM

$StartVM.Status

Always test it out first to make sure this runbook actually works in your environment. After you have confirmed it works, publish it.

2.) Add a webhook.

Just click on Add Webhook from your runbook.

Select the option to Create new webhook. Then give it a name, make sure it’s enabled, set an expiration date, and copy the URL. Then click ok.

3.) Open PowerShell ISE locally on your machine.

In the editor add the following:

$URI = 'the webhook url you copied earlier in the azure portal here'

Invoke-WebRequest -Method Post -Uri $URI -UseBasicParsing

Save this file.

4.) Conversion.

Now install the module we will use to convert the ps1 file to an executable your business user can use. For this install the module called ps2exe by using the cmdlet Install-Module ps2exe. Then wait a couple of seconds and click on ‘Yes to all’.

After you install it. Run win-ps2exe in the next line.

This will give you a GUI if you don’t want to do it the PowerShell way.

Find your previous file, give the new file a name, an icon. You know what, actually read this first to understand what’s going on: Win-PS2EXE/README.md at master · MScholtes/Win-PS2EXE · GitHub.

And NOTE: It is probably not a good idea to store passwords in plain text in that ps1 file as mentioned here as well: Convert a PowerShell script into an EXE with PS2EXE and WIN-PS2EXE – 4sysops.