Automatically starting and stopping D365 virtual machines

 In Blog

Have you ever started your day ready to conquer the world, only to realize your Microsoft Dynamics 365 for Finance and Operations development virtual machine is stopped? Now you must find your way through Azure or LCS screens to find your dev VM, start it, and wait for it for fire up. Wouldn’t it have been nice if that VM had started automatically right before the start of your day?
Or you get that email from your boss wanting to “speak with you” when you get a chance. Turns out they’re just received the bill from Microsoft for Azure services, and that Demo VM you spun up (just to try something) has been running for days because someone (yeah, you) forgot to shut it down or to turn on the Azure auto-shutdown feature.
There’s hope! With the help of Azure Automation Accounts and Logic Apps, we can quickly and easily set up a schedule to stop and start those VMs.

Setting up Azure

The first step is to create an Azure Automation Account. To do that, head to the Azure portal, Automation Accounts. Click on Add, enter the Name, Subscription, Resource Group, and Location. Then hit Create.

Creating an Azure automation account

Creating the Azure automation account

After the Automation Account is created, the next step is to create a Runbook. The runbook will contain the commands to either start the Azure Virtual Machine. To create the runbook, click on the Runbooks link, and a list of runbooks is displayed.

Creating a new Azure runbook

Creating a new runbook

There are a few tutorial runbooks listed at the top, and as you can see, there are 3 runbooks that I have created. To create a new runbook, click on Add a runbook, then click on Create a new runbook in the selection list.

Naming the Azure runbook

Naming the runbook

Give the runbook a name, and under Runbook type select Powershell, then click on Create. This will take you into the runbook canvas for a Powershell runbook. On the left you can explore the multitude of commands available related to Azure automations. Paste the following into the canvas:
Param
(
[Parameter (Mandatory= $false)] [object] $Webhookdata
)

$Conn = Get-AutomationConnection -Name AzureRunAsConnection
$null = Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

# If runbook was called from Webhook, WebhookData will not be null.
if ($WebhookData)
{

# Retrieve VM’s from Webhook request body
$vms = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)

# Start each virtual machine
foreach ($vm in $vms)
{
Start-AzureRMVM -Name $vm.Name -ResourceGroup $vm.ResourceGroup
}
}
else {
# Error
write-Error “This runbook is meant to be started from an Azure alert webhook only.”
}

This code will allow us to pass a VM name into the runbook, and issue the Start-AzureRMVM command to start the VM. Click on Save, then Publish, and form will close. In order to use the runbook from Logic Apps (where the scheduling will take place), we need to create a webhook for our runbook. Click on Webhook at the top of the Runbook form. On the form that opens, click on Create new webhook, and give it a Name. Before hitting OK, click on the copy icon next to URL. The URL contains a security token that will allow us to run this from Logic Apps. This is the same as a password, so keep the URL private.   If needed, you can setup security to make sure that only appropriate connections can use this application, but we won’t be discussing that in this post.  Also, this is the only time you will see the URL, so if you don’t copy it somewhere you will need to repeat this step and create a new webhook to use in Logic Apps.

Creating an Azure webhook

Creating a webhook

Logic App setup

Now on to Logic Apps! Go to Logic Apps in the Azure portal, click on Add, give it a Name, Subscription, Resource Group, and Location. Then click on Create.
Our Logic App will have just 2 steps: a Recurrence step and a HTTP step. Add the Recurrence step and provide whatever schedule you would like for your VM to start. In my example, I want the VM to start at 8AM every day. Next, add the HTTP step. Choose the Method of POST. Then copy your webhook URL into the URI section. Lastly, type in the JSON schema information into the Body section as in the screen shot. Name is the name of the VM you want to start, and ResourceGroup is the resource group the VM belongs to. These map to the parameters that were set up in the runbook, so we can pass a VM Name and Resource Group to the runbook.

Azure automation Logic App setup

Logic App setup

That’s it! This same process can easily be used to set up the schedule to stop a VM by using this command in place of the Start-AzureRMVM command in the runbook:
Stop-AzureRMVM -Name $vm.Name -ResourceGroup $vm.ResourceGroup -Force

There’s a lot that can be done with Azure Automations. This is just the start. I’ve taken these automation runbooks and built a PowerApps app that shows me the status of my Azure VMs, and allows me to start and stop them right from my phone.

PowerApp integrated with Azure automation

Sample of a PowerApp to start and stop VMs

Hopefully this gives you some insight into the power of Azure Automation accounts and saves you time (and money) managing your Dynamics 365 for Finance and Operations Virtual Machines.

Showing 3 comments
  • zainab
    Reply

    how did you link runbook and staff you did in azure with the powerapp?

  • Shariq Mohd
    Reply

    What’s the status your logic app is returning? In my case its statusCode: 202.

    What should be the value in parameter and settings in Webhook.

pingbacks / trackbacks

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Contact Us

Have a question? Want more information? Let us know!

Not readable? Change text. captcha txt