Last active
November 27, 2018 14:39
-
-
Save sfrechette/b53b9b7659bd53eec2bfa717a9dd7bf0 to your computer and use it in GitHub Desktop.
Pause Azure SQL DataWarehouses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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