Last active
January 16, 2018 17:06
-
-
Save chestercodes/56222bdaf181b0cca6b5f2c1e6862b1c to your computer and use it in GitHub Desktop.
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 PauseResourcesIfRunning { | |
Param | |
( | |
[Parameter (Mandatory = $true)] | |
[string] $subscriptionName | |
) | |
write-output "Started script with: | |
Subscription - $subscriptionName" | |
$connectionName = "AzureRunAsConnection" | |
try { | |
$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 | |
} | |
} | |
write-output "selecting subscription" | |
Select-AzureRmSubscription -SubscriptionName $subscriptionName | |
write-output "Get all SQL Datawarehouses in the subscription" | |
$dws = Get-AzureRmResource | Where-Object ResourceType -EQ "Microsoft.Sql/servers/databases" | Where-Object Kind -ILike "*datawarehouse*" | |
#Loop through each SQLDW | |
foreach ($dw in $dws) { | |
write-output "found data warehouse:" | |
$rg = $dw.ResourceGroupName | |
$dwc = $dw.ResourceName.split("/") | |
$sn = $dwc[0] | |
$db = $dwc[1] | |
write-output "$rg, $dwc, $sn, $db" | |
$status = Get-AzureRmSqlDatabase -ResourceGroupName $rg -ServerName $sn -DatabaseName $db | Select Status | |
$dwStatus = $status.Status | |
if ($dwStatus -ne "Paused") { | |
write-output "Status is $dwStatus, pause the warehouse" | |
#If the status is not equal to "Paused", pause the SQLDW | |
Suspend-AzureRmSqlDatabase -ResourceGroupName "$rg" -ServerName "$sn" -DatabaseName "$db" | |
} | |
else { | |
write-output "Status is $dwStatus" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment