-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzure Runbook.ps1
More file actions
81 lines (61 loc) · 2.44 KB
/
Azure Runbook.ps1
File metadata and controls
81 lines (61 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
.SYNOPSIS
Executes an SQL stored procedure to update the SMS opt-in status for a student and department.
.DESCRIPTION
TODO
.PARAMETER SqlServer
String name of the SQL Server to connect to.
.PARAMETER SqlServerPort
Integer port to connect to the SQL Server. Default is 1433.
.PARAMETER Database
String name of the SQL Server database to connect to.
.PARAMETER SqlCredentialAsset
Credential asset name containing a username and password with access to the SQL Server.
.PARAMETER WebhookData
Data passed in from a webhook call.
.NOTES
AUTHOR: Wyatt Best
LASTEDIT: 2021-07-14
#>
param(
[parameter(Mandatory = $True)]
[string] $SqlServer,
[parameter(Mandatory = $False)]
[int] $SqlServerPort = 1433,
[parameter(Mandatory = $True)]
[string] $Database,
[parameter(Mandatory = $True)]
[string] $SqlCredentialAsset,
[parameter(Mandatory = $False)]
[object] $WebhookData
)
$errorActionPreference = "Stop"
# Write-Output $WebhookData.RequestBody
# Read parameters from HTTP POST body
$PostData = (ConvertFrom-Json $WebhookData.RequestBody)
$studentId = $PostData.studentId
$departmentCode = $PostData.departmentCode
$optedIn = $PostData.optedIn
Write-Output "Preparing to set opt-in status for $studentId in department $departmentCode to $optedIn."
$SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset
if ($null -eq $SqlCredential) {
throw "Could not retrieve '$SqlCredentialAsset' credential asset. Check that you created this first in the Automation service."
}
# Get the username and password from the SQL Credential
$SqlUsername = $SqlCredential.UserName
$SqlPass = $SqlCredential.GetNetworkCredential().Password
# Open the SQL connection
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
$Conn.Open()
# Define the SQL command to run
$Cmd = new-object system.Data.SqlClient.SqlCommand("[custom].[CadenceUpdateSMSOpt] $studentId, $departmentCode, $optedIn, 'Cadence'", $Conn)
$Cmd.CommandTimeout = 30
# Execute the SQL command
$Ds = New-Object system.Data.DataSet
$Da = New-Object system.Data.SqlClient.SqlDataAdapter($Cmd)
[void]$Da.fill($Ds)
# Output the count
$Rowcount = $Ds.Tables.Column1
Write-Output "Done with rowcount: $Rowcount"
# Close the SQL connection
$Conn.Close()