Share via


Powershell Script to Auto-shutdown Azure Virtual Machine

Question

Tuesday, March 6, 2018 7:38 AM

Hi 

Could someone pls suggest the PowerShell script to Autoshutdown the VM while creation?

All replies (3)

Tuesday, March 6, 2018 9:18 AM âś…Answered | 2 votes

Hi Naresh,

there's no PowerShell Cmdlet to enable auto-shutdown. What you can do is use a small ARM template to enable auto-shutdown (using New-AzureRmResourceGroupDeployment), or use New-AzureRmResource passing in the correct properties (both after the VM has been created).

Option 1: ARM Template

Define your ARM template file as follows:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vm_name": {
            "defaultValue": "YOURVMNAME",
            "type": "String"
        },
        "shutdown_time": {
            "defaultValue": "1900",
            "type": "String"
        },
        "shutdown_timezone": {
            "defaultValue": "W. Europe Standard Time",
            "type": "String"
        }
    },
    "resources": [
        {
            "type": "microsoft.devtestlab/schedules",
            "name": "[concat('shutdown-computevm-',parameters('vm_name'))]",
            "apiVersion": "2016-05-15",
            "location": "[resourceGroup().location]",
            "scale": null,
            "properties": {
                "status": "Enabled",
                "taskType": "ComputeVmShutdownTask",
                "dailyRecurrence": {
                    "time": "[parameters('shutdown_time')]"
                },
                "timeZoneId": "[parameters('shutdown_timezone')]",
                "notificationSettings": {
                    "status": "Disabled",
                    "timeInMinutes": 30
                },
                "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('vm_name'))]"
            }
        }
    ]
}

Change the parameter values as needed and run the following command to import it:

New-AzureRmResourceGroupDeployment -ResourceGroupName YOUR_RG_NAME -Mode Incremental -Force -TemplateFile C:\Path\To\AutoShutdown.json

Option 2: Use properties and New-AzureRmResource

Run this script:

$resourcegroup = "YOUR_RG_NAME"
$vm = "YOURVMNAME"
$shutdown_time = "1900"
$shutdown_timezone = "W. Europe Standard Time"

$properties = @{
    "status" = "Enabled";
    "taskType" = "ComputeVmShutdownTask";
    "dailyRecurrence" = @{"time" = $shutdown_time };
    "timeZoneId" = $shutdown_timezone;
    "notificationSettings" = @{
        "status" = "Disabled";
        "timeInMinutes" = 30
    }
    "targetResourceId" = (Get-AzureRmVM -ResourceGroupName $resourcegroup -Name $vm).Id
}

New-AzureRmResource -ResourceId ("/subscriptions/{0}/resourceGroups/{1}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{2}" -f (Get-AzureRmContext).Subscription.Id, $resourcegroup, $vm) -Location (Get-AzureRmVM -ResourceGroupName $resourcegroup -Name $vm).Location -Properties $properties -Force

Floris van der Ploeg - www.florisvanderploeg.com

If my post was helpfull, remember to click the "Propose as answer" button.


Wednesday, March 7, 2018 11:05 AM

Thanks. Its useful to know. Hopefully we will get these options while VM creation also, in future.


Wednesday, March 7, 2018 2:25 PM

Hi Naresh,

a small additional note; I created a powershell script to enable/disable auto-shutdown:

https://gallery.technet.microsoft.com/Enable-or-disable-auto-c7837c84

Floris van der Ploeg - www.florisvanderploeg.com

If my post was helpfull, remember to click the "Propose as answer" button.