Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. workflow PauseResourcesIfRunning {
  2. Param
  3. (
  4. [Parameter (Mandatory = $true)]
  5. [string] $subscriptionName
  6. )
  7.  
  8. write-output "Started script with:
  9. Subscription - $subscriptionName"
  10.  
  11. $connectionName = "AzureRunAsConnection"
  12. try {
  13. $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
  14.  
  15. "Logging in to Azure..."
  16. Add-AzureRmAccount `
  17. -ServicePrincipal `
  18. -TenantId $servicePrincipalConnection.TenantId `
  19. -ApplicationId $servicePrincipalConnection.ApplicationId `
  20. -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
  21. }
  22. catch {
  23. if (!$servicePrincipalConnection) {
  24. $ErrorMessage = "Connection $connectionName not found."
  25. throw $ErrorMessage
  26. }
  27. else {
  28. Write-Error -Message $_.Exception
  29. throw $_.Exception
  30. }
  31. }
  32.  
  33. write-output "selecting subscription"
  34. Select-AzureRmSubscription -SubscriptionName $subscriptionName
  35.  
  36. write-output "Get all SQL Datawarehouses in the subscription"
  37. $dws = Get-AzureRmResource | Where-Object ResourceType -EQ "Microsoft.Sql/servers/databases" | Where-Object Kind -ILike "*datawarehouse*"
  38.  
  39. #Loop through each SQLDW
  40. foreach ($dw in $dws) {
  41. write-output "found data warehouse:"
  42. $rg = $dw.ResourceGroupName
  43. $dwc = $dw.ResourceName.split("/")
  44. $sn = $dwc[0]
  45. $db = $dwc[1]
  46. write-output "$rg, $dwc, $sn, $db"
  47. $status = Get-AzureRmSqlDatabase -ResourceGroupName $rg -ServerName $sn -DatabaseName $db | Select Status
  48.  
  49. $dwStatus = $status.Status
  50. if ($dwStatus -ne "Paused") {
  51. write-output "Status is $dwStatus, pause the warehouse"
  52. #If the status is not equal to "Paused", pause the SQLDW
  53. Suspend-AzureRmSqlDatabase -ResourceGroupName "$rg" -ServerName "$sn" -DatabaseName "$db"
  54. }
  55. else {
  56. write-output "Status is $dwStatus"
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment