Skip to content

Instantly share code, notes, and snippets.

@sfrechette
Last active November 27, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfrechette/b53b9b7659bd53eec2bfa717a9dd7bf0 to your computer and use it in GitHub Desktop.
Save sfrechette/b53b9b7659bd53eec2bfa717a9dd7bf0 to your computer and use it in GitHub Desktop.
Pause Azure SQL DataWarehouses
workflow PauseAzureSQLDataWarehouses
{
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
# Get all Azure SQL Data Warehouses in the subscription
$dws = Get-AzureRmResource | Where-Object ResourceType -EQ "Microsoft.Sql/servers/databases" | Where-Object Kind -ILike "*datawarehouse*"
# Loop through each Azure SQL Data Warehouse, getting property values...
foreach($dw in $dws)
{
$rg = $dw.ResourceGroupName
$dwc = $dw.ResourceName.split("/")
$sn = $dwc[0]
$db = $dwc[1]
$status = Get-AzureRmSqlDatabase -ResourceGroupName $rg -ServerName $sn -DatabaseName $db | Select Status
# Check the status
if($status.Status -ne "Paused")
{
#If the status is not equal to "Paused", pause the Azure SQL Data Warehouse
Suspend-AzureRmSqlDatabase -ResourceGroupName "$rg" -ServerName "$sn" -DatabaseName "$db"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment