FreezeResourceGroup.ps1

<#PSScriptInfo
 
.VERSION 1.2
 
.GUID e9cf4243-fdb6-4b4f-b36b-1d40434abc1c
 
.AUTHOR gill.griffiths@uk.nestle.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Azure StopPauseResources Freeze ResourceGroup
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
    .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
    This runbook will stop any VMs that are started, pause any SQL Data Warehouses that are running, pause any Analysis Services instances that are running and disable scheduled runbooks
 
        This runbook requires the following modules:
        - "Azure" and "AzureRM.Resources" modules which are present by default in Azure Automation accounts
        - "AzureRM.AnalysisServices" module
 
        For detailed documentation and instructions, see:
        <to be provided>
 
    .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-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint


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

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

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

    if ($vmpowerstate -eq 'VM Running')
        {
            $outputmessage = "Stopping VM " + $vm.name + " in Resource Group " + $ResourceGroup
            write-output $outputmessage
            Stop-AzureRmVM -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-AzureRmSqlServer -ResourceGroupName $ResourceGroup

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

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

        if ($sqldbedition -eq 'DataWarehouse' -and $sqldbstatus -eq 'Online')
            {
                $outputmessage = "Pausing SQL Data Warehouse " + $sqldb.databasename + " in Resource Group " + $ResourceGroup
                write-output $outputmessage
                Suspend-AzureRmSqlDatabase -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-AzureRmAnalysisServicesServer -ResourceGroupName $ResourceGroup

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

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


# DISABLE SCHEDULED RUNBOOKS
# get the automation account
$AutomationAccount = Get-AzureRmAutomationAccount -resourcegroupname $ResourceGroup

# get all the schedules
$schedules = Get-AzureRmAutomationSchedule -resourcegroupname $ResourceGroup -automationaccountname $AutomationAccount.AutomationAccountName

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