Need PowerShell script to disable auto-shutdown option for all Azure Virtual Machines in that are exists in multiple resource groups

Mounika Y 0 Reputation points
2023-10-25T10:55:34.52+00:00

Hi Team,

I need a PowerShell script to disable auto-shutdown option for all my existing Virtual Machines in different resource groups. There is no auto-shutdown related PowerShell commands found in the Microsoft documents. Can some one please help ASAP.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,585 questions
{count} votes

2 answers

Sort by: Most helpful
  1. kangupt-MSFT 20 Reputation points Microsoft Employee
    2023-10-30T07:51:36.18+00:00

    Hello Mounika,

    Thank you for contacting Azure Community Forum. You can try to change the script below as this is for 1 resource group and make changes according to your environment. In the script.ps1 you can give the command "Set-AzureRmVMAutoShutdown"

    #Azure Subscription I want to use
    $subscriptionId = "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    #Resource Group my VMs are in
    $resourceGroup = "test-azurevms-rg"
    
    #Select the right Azure subscription
    Set-AzContext -Subscription $subscriptionId
    
    #Get all Azure VMs which are in running state and are running Windows
    $myAzureVMs = Get-AzVM -ResourceGroupName $resourceGroup -status | Where-Object {$_.PowerState -eq "VM running" -and $_.StorageProfile.OSDisk.OSType -eq "Windows"}
    
    #Run the scirpt again all VMs in parallel
    $myAzureVMs | ForEach-Object -Parallel {
        $out = Invoke-AzVMRunCommand `
            -ResourceGroupName $_.ResourceGroupName `
            -Name $_.Name  `
            -CommandId 'RunPowerShellScript' `
            -ScriptPath .\script.ps1 
        #Formating the Output with the VM name
        $output = $_.Name + " " + $out.Value[0].Message
        $output   
    }
    
    
    

    This is from a 3rd party article as below:

    https://www.thomasmaurer.ch/2021/03/how-to-run-scripts-against-multiple-azure-vms-by-using-run-command/

    -Kanika


  2. Graeme S 0 Reputation points
    2024-02-07T03:07:00.4533333+00:00

    Was also looking for a similar script, Google lead me here but the example script doesn't satisfy the OP question so expanded it as required.

    Try script below (save as ps1 file) run from command line like this.

    & '.\Azure - Control Auto-Shutdown.ps1' -SubscriptionId "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" -AutoShutdownStatus "Disabled"
    
    #Script to set auto shutdown status on Azure Virtual Machines in a subscription
    param (
         [Parameter(Mandatory=$true)]
         [string]$SubscriptionId,
         [Parameter(Mandatory=$true)]
         [ValidatePattern("Enabled|Disabled")]
         [string]$AutoShutdownStatus = "Enabled"
    
    )
    
    function Login()  
    {  
        $context = Get-AzContext  
      
        if (!$context)   
        {  
            Connect-AzAccount  
        }   
        else   
        {  
            Write-Host "Already connected"  
        }  
    }  
    
    Login
    
    Write-Host "Setting Auto Shutdown Status to $AutoShutdownStatus"
    
    Set-AzContext -Subscription $subscriptionId | Out-Null
    
    $resourceGroups = Get-AzResourceGroup
    
    $resourceGroups | ForEach-Object {
        # could include a filter here, e.g. -status | Where-Object {$_.PowerState -eq "VM running" -and $_.StorageProfile.OSDisk.OSType -eq "Windows"}
        $myAzureVMs = Get-AzVM -ResourceGroupName $_.ResourceGroupName
    
        $rgName = $_.ResourceGroupName
    
        $myAzureVMs | ForEach-Object  {
    
            $vmName = $_.Name
    
            $ScheduledShutdownResourceId = "/subscriptions/$SubscriptionId/resourceGroups/$rgName/providers/microsoft.devtestlab/schedules/shutdown-computevm-$vmName"
            $ShutdownResource = Get-AzResource -ResourceId $ScheduledShutdownResourceId
    
            if ($ShutdownResource -ne $null)
            { 
                if ($ShutdownResource.Properties.Status -eq $AutoShutdownStatus) {
                    #No change needed
                    $changeType = "no change"
                }
                else {
                    $changeType = "updated to $AutoShutdownStatus"
                    $ShutdownResource.Properties.Status = $AutoShutdownStatus
                    $ShutdownResource | Set-AzResource -Force | Out-Null
                    }
    
                Write-Host "Current status for $vmName is $($ShutdownResource.Properties.Status) - $changeType"
            }
            else {
                Write-Host "No current auto shutdown status for $vmName "
            }
    
        }
    }
    
    0 comments No comments