question

Phil-6516 avatar image
0 Votes"
Phil-6516 asked tbgangav-MSFT answered

Azure Policy - Enable Auto Shutdown at VMs

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

tbgangav-MSFT avatar image
1 Vote"
tbgangav-MSFT answered

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


image.png (33.2 KiB)
image.png (40.0 KiB)
image.png (26.9 KiB)
image.png (29.6 KiB)
image.png (44.2 KiB)
image.png (31.6 KiB)
image.png (42.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.