The policy restricts access by specifying allowed or denied actions. However, if you want to schedule a machine reboot, consider using an Automation Account.
1)Create an Automation account and Runbook
2)Connect to Azure with system-assigned managed identity (provide contributor rights to Managed identity).
Below is the PowerShell script:
$AzureContext = (Connect-AzAccount -Identity).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
# Connect to Azure (if not already connected)
#Connect-AzAccount
Select-AzSubscription -Subscription "SubscrpionName"
# Define the tag key and value
$TagKey = "Environment"
$TagValue = "dev-Autoshutdown"
# Get all VMs that have the specified tag value
$VMs = Get-AzVM | Where-Object { $_.Tags[$TagKey] -eq $TagValue }
# Stop the VMs
foreach ($VM in $VMs) {
Write-Host "Stopping VM: $($VM.Name) in Resource Group: $($VM.ResourceGroupName)"
#For Stop use the Below Command in the Stop Dev VMs Run book
Stop-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -Force
#For start use the Below Command in the Stop Dev VMs Run book
#Start-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName
}
Write-Host "All dev-test tagged VMs have been powered off."
Please let me know if you face any challenge here, I can help you to resolve this issue further
Provide your valuable Comments.
Please do not forget to "Accept the answer” and “upvote it” wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.