@r001 - Welcome to Microsoft Q&A and thanks for reaching out to us.
In order to set up automation of Azure Virtual Machines in Production you can use Azure Automation Runbooks.
You can follow below for a high-level steps to achieve the same:
- Create an Azure Automation account in your subscription and create a new runbook in the account.
- Write a PowerShell script to take a snapshot of the VMs and tag them with the current date.
- Schedule the Runbook to run daily at a specific time.
- Write another PowerShell script to remove the snapshots that were created by the Runbook.
- Schedule the second Runbook to run daily at a specific time.
Here is an example PowerShell script that you can use to take a snapshot of a VM and tag it with the current date:
# Get the VM object
$vm = Get-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM"
# Get the current date
$date = Get-Date -Format "yyyy-MM-dd"
# Create the snapshot
$snapshot = New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -CreateOption Copy -Location $vm.Location -Tags @{ Created=$date } New-AzSnapshot -Snapshot $snapshot -SnapshotName "mySnapshot" -ResourceGroupName "myResourceGroup"
And here is an example PowerShell script that you can use to remove the snapshots that were created by the Runbook:
# Get the snapshots that were created by the Runbook
$snapshots = Get-AzSnapshot -ResourceGroupName "myResourceGroup" | Where-Object { $_.Tags.Created -eq (Get-Date -Format "yyyy-MM-dd") }
# Remove the snapshots
$snapshots | Remove-AzSnapshot
Please note that these scripts are just examples, and you may need to modify them to fit your specific requirements. Also, make sure to test the scripts in a non-production environment before running them in production.
Hope this helps. and please feel free to reach out if you have any further questions.
If the above response was helpful, please feel free to "Accept as Answer" and click "Yes" so it can be beneficial to the community.