Skip to content

Instantly share code, notes, and snippets.

@wilfriedwoivre
Created September 3, 2019 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilfriedwoivre/8fc8040bbc655bd247de68e12e99f0e2 to your computer and use it in GitHub Desktop.
Save wilfriedwoivre/8fc8040bbc655bd247de68e12e99f0e2 to your computer and use it in GitHub Desktop.
Azure Policy Auto Shutdown
{
"mode": "Indexed",
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Compute/virtualMachines"
},
{
"field": "[concat('tags[', parameters('tagAutoShutdownEnabled'), ']')]",
"exists": "true"
},
{
"field": "[concat('tags[', parameters('tagAutoShutdownHour'), ']')]",
"exists": "true"
},
{
"field": "[concat('tags[', parameters('tagAutoShutdownTimeZone'), ']')]",
"exists": "true"
}
]
},
"then": {
"effect": "deployIfNotExists",
"details": {
"type": "Microsoft.DevTestLab/schedules",
"name": "[concat('shutdown-computevm-', field('name'))]",
"existenceCondition": {
"allOf": [
{
"field": "tags.AutoShutdown-Enabled",
"equals": "[field('tags.AutoShutdown-Enabled')]"
},
{
"field": "tags.AutoShutdown-Hour",
"equals": "[field('tags.AutoShutdown-Hour')]"
},
{
"field": "tags.AutoShutdown-TimeZone",
"equals": "[field('tags.AutoShutdown-TimeZone')]"
}
]
},
"roleDefinitionIds": [
"/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
],
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmName": {
"type": "string"
},
"location": {
"type": "string"
},
"status": {
"type": "string",
"allowedValues": [
"Enabled",
"Disabled"
]
},
"shutdownHour": {
"type": "string"
},
"timeZone": {
"type": "string"
}
},
"variables": {
"shutdownHour": "[replace(parameters('shutdownHour'), ':', '')]"
},
"resources": [
{
"type": "Microsoft.DevTestLab/schedules",
"apiVersion": "2016-05-15",
"name": "[concat('shutdown-computevm-', parameters('vmName'))]",
"location": "[parameters('location')]",
"tags": {
"AutoShutdown-Enabled": "[parameters('status')]",
"AutoShutdown-TimeZone": "[parameters('timeZone')]",
"AutoShutdown-Hour": "[parameters('shutdownHour')]"
},
"properties": {
"status": "[parameters('status')]",
"taskType": "ComputeVmShutdownTask",
"dailyRecurrence": {
"time": "[variables('shutdownHour')]"
},
"timeZoneId": "[parameters('timeZone')]",
"notificationSettings": {
"status": "Disabled",
"timeInMinutes": 30
},
"targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]"
}
}
],
"outputs": {
"policy": {
"type": "string",
"value": "[concat('Autoshutdown configured for VM', parameters('vmName'))]"
}
}
},
"parameters": {
"vmName": {
"value": "[field('name')]"
},
"location": {
"value": "[field('location')]"
},
"status": {
"value": "[field('tags.AutoShutdown-Enabled')]"
},
"shutdownHour": {
"value": "[field('tags.AutoShutdown-Hour')]"
},
"timeZone": {
"value": "[field('tags.AutoShutdown-TimeZone')]"
}
}
}
}
}
}
},
"parameters": {
"tagAutoShutdownEnabled": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": null
},
"defaultValue": "AutoShutdown-Enabled"
},
"tagAutoShutdownTimeZone": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": null
},
"defaultValue": "AutoShutdown-TimeZone"
},
"tagAutoShutdownHour": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": null
},
"defaultValue": "AutoShutdown-Hour"
}
}
}
@sofianlak
Copy link

il est possible de lancer le autoshutdown à un jour particulier de la semaine ? par exemple arrêter les machines tous les vendredi à une certaine heure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment