-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slack-Integration.ps1
229 lines (196 loc) · 8.34 KB
/
Slack-Integration.ps1
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
param (
[Object] $WebhookData
)
function Get-SlackParameter {
<#
.Synopsis
This function takes the input parameters to a Webhook call by the Slack service. The function translates the query
string, provided by the Slack service, and returns a PowerShell HashTable of key-value pairs. Azure Automation accepts
a $WebhookData input parameter, for Webhook invocations, and you should pass the value of the RequestBody property
into this function's WebhookPayload parameter.
.Parameter WebhookPayload
This parameter accepts the Azure Automation-specific $WebhookData.RequestBody content, which contains
input parameters from Slack. The function parses the query string, and returns a HashTable of key-value
pairs, that represents the input parameters from a Webhook invocation from Slack.
eg. var1=value1&var2=value2&var3=value3
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $WebhookPayload
)
$ParamHT = @{ };
$Params = $WebhookPayload.Split('&');
foreach ($Param in $Params) {
try {
$Param = $Param.Split('=');
$ParamHT.Add($Param[0], [System.Net.WebUtility]::UrlDecode($Param[1]))
}
catch {
Write-Warning -Message ('Possible null parameter value for {0}' -f $Param[0]);
}
}
Write-Output -InputObject $ParamHT;
}
$SlackParams = Get-SlackParameter -WebhookPayload $WebhookData.RequestBody;
function Send-SlackMessage {
<#
.Synopsis
This function sens a message to a Slack channel.
.Description
This function sens a message to a Slack channel. There are several parameters that enable you to customize
the message that is sent to the channel. For example, you can target the message to a different channel than
the Slack incoming webhook's default channel. You can also target a specific user with a message. You can also
customize the emoji and the username that the message comes from.
For more information about incoming webhooks in Slack, check out this URL: https://api.slack.com/incoming-webhooks
.Parameter Message
The -Message parameter specifies the text of the message that will be sent to the Slack channel.
.Parameter Channel
The name of the Slack channel that the message should be sent to.
- You can specify a channel, using the syntax: #<channelName>
- You can target the message at a specific user, using the syntax: @<username>
.Parameter $Emoji
The emoji that should be displayed for the Slack message. There is an emoji cheat sheet available here:
http://www.emoji-cheat-sheet.com/
.Parameter Username
The username that the Slack message will come from. You can customize this with any string value.
.Links
https://api.slack.com/incoming-webhooks - More information about Slack incoming webhooks
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Message
, [Parameter(Mandatory = $false)]
[string] $Channel = ''
, [Parameter(Mandatory = $false)]
[string] $Emoji = ''
, [Parameter(Mandatory = $false)]
[string] $Username = 'Azure Automation'
)
### Build the payload for the REST method call
$RestBody = @{
text = $Message;
username = $Username;
icon_emoji = $Emoji;
}
### Build the command invocation parameters for Splatting on Invoke-RestMethod
$RestCall = @{
Body = (ConvertTo-Json -InputObject $RestBody);
Uri = Get-AutomationVariable -Name SlackIncomingWebhook;
ContentType = 'application/json';
Method = 'POST';
}
### Invoke the REST method call to the Slack service's webhook.
Invoke-RestMethod @RestCall;
Write-Verbose -Message 'Sent message to Slack service';
}
Send-SlackMessage -Message 'Wroom!';
$null = Add-AzureRmAccount -Credential (Get-AutomationPSCredential -Name AzureAdmin)
if ($SlackParams.Text -like 'go*') {
#¡ready
Send-SlackMessage -Message 'Preparing Crazy Stuff';
$SlackTextArr = $SlackParams.Text.Split(' ');
$resourceGroupName = $SlackTextArr[1]
$location = $SlackTextArr[2]
$vaultName = $SlackTextArr[3]
$pwd = $SlackTextArr[4]
#set!
New-AzureRmResourceGroup –Name $resourceGroupName –Location $location
Send-SlackMessage -Message 'Created RG'
New-AzureRmKeyVault -VaultName $vaultName -ResourceGroupName $resourceGroupName -Location $location
Send-SlackMessage -Message 'Created Vault'
Set-AzureRmKeyVaultAccessPolicy -VaultName $vaultname -ObjectId '0a1e3ccc-1235-4342-96e4-ae8fd8090ad0' -PermissionsToSecrets get,set
Set-AzureRmKeyVaultAccessPolicy -EnabledForTemplateDeployment -VaultName $vaultName
Set-AzureKeyVaultSecret -VaultName $vaultName -Name 'administratorLoginPassword' -SecretValue (ConvertTo-SecureString $pwd -AsPlainText -Force)
Send-SlackMessage -Message 'Prepared Vault'
#¡go!
$parameters = @{
"Name" = "bbbb-is-the-word"
"Mode" = "Incremental"
"ResourceGroupName" = "$resourceGroupName"
"TemplateUri"="https://raw.githubusercontent.com/4c74356b41/bbbb-is-the-word/master/ARM/parent.json"
"SITENAME-PRIMARY"="$resourceGroupName-1s-the-word"
"SITENAME-SECONDARY"="$resourceGroupName-1s-the-wordd"
"SITENAME-FUNCTIONS"="$resourceGroupName-funct1ons"
"HOSTINGPLAN-PRIMARY"="Plan-a"
"HOSTINGPLAN-SECONDARY"="Plan-b"
"SKU"="F1"
"ADMINISTRATORLOGIN"="dba"
"VAULTNAME"="$vaultName"
"SECRETNAME"="administratorLoginPassword"
"DATABASENAME"="database"
"SERVERNAME"="$resourceGroupName-sq1-pr1"
"SERVERREPLICANAME"="$resourceGroupName-sq1-rep1"
"STORAGEACCOUNTSTUB"= $resourceGroupName
}
Send-SlackMessage -Message 'Starting Crazy Stuff';
try {
$job = New-AzureRmResourceGroupDeployment @parameters
}
catch {
Send-SlackMessage -Message ' >>> ### !Failed! ### <<<'
Send-SlackMessage -Message (ConvertTo-Json -InputObject $error)
}
Send-SlackMessage -Message (ConvertTo-Json -InputObject $job.outputs.values)
return;
}
if ($SlackParams.Text -like 'flip*') {
Send-SlackMessage -Message 'Flipping'
$SlackTextArr = $SlackParams.Text.Split(' ');
$data = @{
AzureResourceGroup = $SlackTextArr[1];
Tag = $SlackTextArr[2];
Shutdown = $SlackTextArr[3];
}
$job = Start-AzureRmAutomationRunbook –AutomationAccountName "AzureRmAccount" –Name "Shutdown-Start-VMs-By-Resource-Group" -ResourceGroupName "AzureRm" –Parameters $data -wait
foreach ($result in $job) {Send-SlackMessage -Message $result}
return;
}
if ($SlackParams.Text -like 'test*') {
Send-SlackMessage -Message 'Flipping Testo'
$SlackTextArr = $SlackParams.Text.Split(' ');
$data = @{
AzureResourceGroup = $SlackTextArr[1];
Tag = $SlackTextArr[2];
Shutdown = $SlackTextArr[3];
}
$job = Start-AzureRmAutomationRunbook –AutomationAccountName "AzureRmAccount" –Name "sample" -ResourceGroupName "AzureRm" –Parameters $data -wait
foreach ($result in $job) {Send-SlackMessage -Message $result}
return;
}
if ($SlackParams.Text -eq 'arml') {
Write-Verbose -Message 'Listing Microsoft Azure Resource Manager (ARM) Resource Groups';
Send-SlackMessage -Message ((Get-AzureRmResourceGroup).ResourceGroupName -join "`n");
return;
}
if ($SlackParams.Text -like 'armd*') {
try {
$ResourceGroupName = $SlackParams.Text.Split(' ')[1];
Write-Verbose -Message ('Deleting ARM Resource Group named {0}' -f $ResourceGroupName);
Remove-AzureRmResourceGroup -Name $ResourceGroupName -Force -ErrorAction Stop;
Send-SlackMessage -Message ('Azure Automation successfully deleted the ARM Resource Group named {0}' -f $ResourceGroupName)
}
catch {
throw ('Error occurred while deleting ARM Resource Group {0}: {1}' -f $ResourceGroupName, $PSItem.Exception.Message);
}
return;
}
if ($SlackParams.Text -like 'armn*') {
try {
$SlackTextArr = $SlackParams.Text.Split(' ');
$ResourceGroup = @{
Name = $SlackTextArr[1];
Location = $SlackTextArr[2];
Force = $true;
}
Write-Verbose -Message ('Creating ARM Resource Group named {0} in the {1} region.' -f $ResourceGroup.Name, $ResourceGroup.Location);
New-AzureRmResourceGroup @ResourceGroup -ErrorAction Stop;
Send-SlackMessage -Message ('Azure Automation successfully create the ARM Resource Group named {0} in region {1}' -f $ResourceGroup.Name, $ResourceGroup.Location)
}
catch {
throw ('Error occurred while creating ARM Resource Group {0}: {1}' -f $ResourceGroup.Name, $PSItem.Exception.Message);
}
return;
}
Send-SlackMessage -Message ('No Slack command found in Azure Automation Runbook: {0}' -f $SlackParams.Text.Split(' ')[0]);