Azure Policy - Enable Auto Shutdown at VMs

Philipp Gerber 251 Reputation points
2022-05-02T21:27:13.21+00:00

0

it is possible to activate the Azure VM Autoshutdown Function with an Azure Policy?

I found always articles with an DevTest Lab. But i would like to use an AZure Policy at the Production Subscription to can found VMs without an Azure AutoShutdown Configuration.

Thanks.

Best Regards,
Phil

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
797 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,386 Reputation points
    2022-05-14T08:17:08.48+00:00

    Hi @PhilippGerber-6516,

    Yes, it is possible to activate the Azure VM AutoShutdown function with an Azure Policy. I have tested it in my subscription with this policy. For illustration, you may refer all the below screenshots.

    To clarify around "DevTest lab" that you have mentioned, currently the schedule that gets created when we enable AutoShutdown function for an Azure VM is of type "Microsoft.devtestlab/schedules" hence it is mentioned in the above referred policy in the same way but it works for Azure VMs.

    To double check the same, I have created a new Azure VM and enabled AutoShutdown function while creating the VM itself and then I have tried to get the type of the AutoShutdown schedule using Azure PowerShell as shown below.

    $subscriptionId = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
    $ResourceGroupName = "xxxxxxxxxx"
    $VMName = "xxxxxxxxxxxxx "
    $shutdownResource = Get-AzResource -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/microsoft.devtestlab/schedules/shutdown-computevm-$VMName"
    $shutdownResource.Properties
    $shutdownResource.Properties.dailyRecurrence

    201932-image.png

    201907-image.png

    Later I have assigned the above mentioned policy and then I have created another Azure VM without enabling AutoShutdown function and have waiting until the schedule time thats mentioned in the policy and then I could see that AutoShutdown is enabled automatically with the help of the policy.

    201878-image.png

    201962-image.png

    201952-image.png

    201916-image.png

    201971-image.png

    1 person found this answer helpful.
    0 comments No comments