FreezeResourceGroup.ps1

<#PSScriptInfo
 
.VERSION 1.4
 
.GUID e9cf4243-fdb6-4b4f-b36b-1d40434abc1c
 
.AUTHOR gill.griffiths@uk.nestle.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Azure StopPauseResources Freeze ResourceGroup
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES Az.Accounts,Az.Automation,Az.Compute,Az.Sql,Az.AnalysisServices
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
1.4 Changed to use new Az modules instead of AzureRm ones, also fixes failure if there is more than one automation account
 
.PRIVATEDATA
 
#>
 



<#
    .SYNOPSIS
        This Azure Automation runbook 'freezes' a resource group.
        It was designed to be called from an action group assigned to a budget alert i.e. once all the budget has been consumed for
        the resource group then freeze the resource group, but it can also be used standalone.
 
.DESCRIPTION
Freeze Resource Group
 
    .PARAMETER ResourceGroup
        The name of the Resouce Group in which the resources will be stopped/paused.
 
    .EXAMPLE
        For testing examples, see the documentation at:
        <to be provided>
 
    .INPUTS
        None.
 
    .OUTPUTS
        Human-readable informational and error messages produced during the job. Not intended to be consumed by another runbook.
#>



param(
    [Parameter(Position=0,mandatory=$true)]
    [string]$ResourceGroup
)


# AUTHENTICATE WITH YOUR AUTOMATION ACCOUNT
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint


# DISABLE SCHEDULED RUNBOOKS
# Get the automation accounts
$AutomationAccounts = Get-AzAutomationAccount -resourcegroupname $ResourceGroup

# For each automation account
Foreach ($AutomationAccount in $AutomationAccounts)
    {
       # Get all the schedules
        $schedules = Get-AzAutomationSchedule -resourcegroupname $ResourceGroup -automationaccountname $AutomationAccount.AutomationAccountName

        # Disable all schedules
        Foreach ($schedule in $schedules)
            {
                if ($schedule.isenabled -eq 'True')
                    {
                        $outputmessage = "`n" + "Disabling schedule " + $schedule.name + " in Resource Group " + $ResourceGroup
                        write-output $outputmessage
                        Set-AzAutomationSchedule -resourcegroupname $ResourceGroup -automationaccountname $AutomationAccount.AutomationAccountName -name $schedule.name -isenabled 0
                    }
            }
    }


# STOP ANY VMS THAT ARE STARTED
# Get all the VMs in the resource group
$vms = Get-AzVM -ResourceGroupName $ResourceGroup

# Stop any VM that is running
Foreach ($vm in $vms)
    {
        $vmstatus = Get-AzVM -ResourceGroupName $ResourceGroup -name $vm.name -Status

        $vmpowerstate = $vmstatus.Statuses[1].DisplayStatus

        if ($vmpowerstate -eq 'VM Running')
            {
               $outputmessage = "`n" + "Stopping VM " + $vm.name + " in Resource Group " + $ResourceGroup
               write-output $outputmessage
               Stop-AzVM -ResourceGroupName $ResourceGroup -Name $vm.name -Force
            }
    }


# PAUSE ANY SQL DATA WAREHOUSES THAT ARE RUNNING
# Get all the SQL databases and data warehouses in the resource group
$sqlservers = Get-AzSqlServer -ResourceGroupName $ResourceGroup

# Pause any SQL server data warehouse that is running
Foreach ($sqlserver in $sqlservers)
    {
        $sqldbs = Get-AzSqlDatabase -ResourceGroupName $ResourceGroup -servername $sqlserver.servername

        Foreach ($sqldb in $sqldbs)
            {
                 $sqldbedition = $sqldb.Edition
                $sqldbstatus = $sqldb.Status

                if ($sqldbedition -eq 'DataWarehouse' -and $sqldbstatus -eq 'Online')
                    {
                        $outputmessage = "`n" + "Pausing SQL Data Warehouse " + $sqldb.databasename + " in Resource Group " + $ResourceGroup
                        write-output $outputmessage
                        Suspend-AzSqlDatabase -ResourceGroupName $ResourceGroup -ServerName $sqldb.servername -DatabaseName $sqldb.databasename
                    }
            }
    }


# PAUSE ANY ANALYSIS SERVICES INSTANCES THAT ARE RUNNING
# Get all the Analysis Services in the resource group
$ass = Get-AzAnalysisServicesServer -ResourceGroupName $ResourceGroup

# Pause any Analysis Service that is running
Foreach ($as in $ass)
    {
        $asstate = $as.State

        if ($asstate -eq 'Succeeded')
            {
                $outputmessage = "`n" + "Pausing Analysis Service " + $as.name + " in Resource Group " + $ResourceGroup
                write-output $outputmessage
                Suspend-AzAnalysisServicesServer �ResourceGroupName $ResourceGroup �Name $as.name
            }
    }