Azure Runbook to control D365FO Azure VM

Azure Runbook service can be used to run power shell commands related to azure. This helps automate azure related maintenance tasks.

As a PoC I created a runbook to stop a VM. Dynamics 365 machines are deployed in Azure but controlled through LCS portal. LCS portal is a good feature rich website but there is a lot of clutter and loading of pages involved until you reach the section to start/stop a specific machine. All I wanted to do a quick stop of my machine from my mobile while I don't need it. Running a machine unnecessary increases my Azure bill.

As a first step, I created Azure runbook script which actually do the job of stopping the machine. This can be plugged into some mobile device related trigger at later point.

First item to start is to create a new Azure automation account. Go to Azure portal and then select 'All Services' > 'Management + governance' > Automation accounts



Then click 'Add' button, fill up details like name, resource group, location etc. to create a new account. Once the account is created, click on it to open it. Then select 'Runbooks' from 'Process automation' area on left side.



Click 'Create a runbook' button. Provide name, description of the runbook. Choose 'PowerShell' in Runbook type as I need to run PowerShell scripts in runbook. then click 'Create' button. Once the runbook is created, click edit button to start writing powershell script.

Paste below script in the runbook.

Param(
[string]$VMName,
[string]$ResourceGroupName
)

Write-Output "VM name is:"
Write-Output $VMName

Write-Output "Group name is:"
Write-Output $ResourceGroupName

#Get connection settings
$connection = Get-AutomationConnection -Name 'AzureRunAsConnection'

#Log-in
Login-AzureRmAccount -ServicePrincipal -Tenant $connection.TenantID `
-ApplicationID $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint

#Stop VM
Stop-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName -Force

Change above code as required by you. Once the code is done, save and publish the runbook. I have given two variables in the runbook code to make it generic. When you'll run it (Start button), system asks for the VM name and its resource group name. These variables will be used when the runbook will be called from other systems (mobile device). But that I'll cover in some other blog.

Comments

  1. If you're trying to lose pounds then you need to start using this totally brand new custom keto meal plan.

    To produce this service, certified nutritionists, fitness trainers, and professional chefs have joined together to develop keto meal plans that are powerful, convenient, economically-efficient, and delicious.

    From their first launch in early 2019, thousands of clients have already remodeled their figure and well-being with the benefits a great keto meal plan can give.

    Speaking of benefits: in this link, you'll discover 8 scientifically-proven ones given by the keto meal plan.

    ReplyDelete

Post a Comment

Popular Posts