Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Outputs the result of a SQL command.
  4.  
  5. .DESCRIPTION
  6. In order for this runbook to work, the SQL Server must be accessible from the runbook worker
  7. running this runbook. Make sure the SQL Server allows incoming connections from Azure services
  8. by selecting 'Allow Windows Azure Services' on the SQL Server configuration page in Azure.
  9.  
  10. This runbook also requires an Automation Credential asset be created before the runbook is
  11. run, which stores the username and password of an account with access to the SQL Server.
  12. That credential should be referenced for the SqlCredential parameter of this runbook.
  13.  
  14. .PARAMETER SqlServer
  15. String name of the SQL Server to connect to.
  16.  
  17. .PARAMETER SqlServerPort
  18. Integer port to connect to the SQL Server on.
  19.  
  20. .PARAMETER Database
  21. String name of the SQL Server database to connect to.
  22.  
  23. .PARAMETER sql
  24. SQL string to execute.
  25.  
  26. .PARAMETER $SqlCredName
  27. String name of the credential asset containing a username and password for the SQL Server
  28.  
  29. .PARAMETER $SmtpCredName
  30. String name of the credential asset containing a username and password for the SMTP server.
  31. #>
  32.  
  33. workflow CheckDatabaseAndSendEmail
  34. {
  35. param(
  36. [parameter(Mandatory=$True)]
  37. [string] $SqlServer = "xxxxxxxxxx.database.windows.net",
  38.  
  39. [parameter(Mandatory=$False)]
  40. [int] $SqlServerPort = 1433,
  41.  
  42. [parameter(Mandatory=$True)]
  43. [string] $Database = "MyDatabaseName",
  44.  
  45. [parameter(Mandatory=$True)]
  46. [string] $sql = "EXECUTE [dbo].[BuildEmailBody] 10",
  47.  
  48. [parameter(Mandatory=$True)]
  49. [string] $SqlCredName = "sqldb",
  50.  
  51. [parameter(Mandatory=$True)]
  52. [string] $SmtpCredName = "smtp"
  53. )
  54.  
  55. # Get the username and password from the SQL Credential
  56. $SqlCredential = Get-AutomationPSCredential -Name $SqlCredName
  57. $SqlUsername = $SqlCredential.UserName
  58. $SqlPass = $SqlCredential.GetNetworkCredential().Password
  59.  
  60. $SmtpCred = Get-AutomationPSCredential -Name $SmtpCredName
  61.  
  62. inlinescript {
  63. $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$using:SqlServer,$using:SqlServerPort;Database=$using:Database;User ID=$using:SqlUsername;Password=$using:SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
  64. $Conn.Open()
  65. $Cmd=new-object System.Data.SqlClient.SqlCommand($using:sql, $Conn)
  66. $Cmd.CommandTimeout=120
  67.  
  68. $Ds=New-Object System.Data.DataSet
  69. $Da=New-Object System.Data.SqlClient.SqlDataAdapter($Cmd)
  70. [void]$Da.fill($Ds)
  71. $html = $Ds.Tables[0].Rows[0][0]
  72. $Conn.Close()
  73.  
  74. if ($html.Length -eq 0) {
  75. Write-Output "No data was found."
  76. } else {
  77. Write-Output $html
  78.  
  79. $To = "me@example.com"
  80. $From = "auto@example.com"
  81. $Subject = "Email from Azure Automation"
  82.  
  83. Send-MailMessage `
  84. -To $To `
  85. -Subject $Subject `
  86. -Body $html `
  87. -UseSsl `
  88. -Port 587 `
  89. -SmtpServer 'smtp.sendgrid.net' `
  90. -From $From `
  91. -BodyAsHtml `
  92. -Encoding ([System.Text.Encoding]::UTF8) `
  93. -Credential $using:SmtpCred
  94.  
  95. Write-Output "Email was sent."
  96. }
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement