I am using the azure runbook to stop a VM if its idle for more than 10 Min
Steps followed
Added user in Managed Identities with a role of contributor.
Created a automation account and tagged the user Assigned ( which was created in earlier step )
Opened the VM and under IAM - add user assigned
then opened Identity ( same VM ) and tagged the above user
Go to automation account and then open runbook and used below script
#Get the target VM by its resource group and name $resourceGroupName = 'ResourceGroup' $vmName = 'VMName'
#Check VM activity and stop if idle for more than 1 hour $threshold = (Get-Date).AddHours(-1)
#Check if the VM is running and idle for more than 1 hour $vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName if ($vm.RuntimeStatus -eq 'VM running' -and $vm.OSProfile.LastBootTime -lt $threshold) { Stop-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -Force }
however its prompting me to login or connect using run Connect-AzAccount to login.
As per my understanding as there is already user tagged under VM, shouldn't it login automatically..
pls excuse my ignorance, if this is already answered some where, please paste the link accordingly.
Thanks