-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.slack.psm1
3505 lines (2864 loc) · 134 KB
/
ps.slack.psm1
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Function Get-slackHelp {
<#
.Synopsis
Get fast help for most useful examples for every function
.Description
Get fast help for most useful examples for every function
.Example
Get-slackHelp help
Get help on help
.Example
gskh -list
Get list of module commands
.Example
gskh -alias
Get list of aliases for the ps.slack module
.Example
gskh message
Get examples for all slackMessage commands
.Example
gskh mess* -p zabbix -short
Get examples for lackMessage look for string zabbix
.Example
gskh -zverb get
Get examples of all get commands
.Example
gskh message set
Get examples for Set-slackMessage
.Example
gskh message -p set
Get examples for *-slackMessage and look for "set""
#>
[CmdletBinding()]
[Alias("gskh")]
Param ($znoun,$zverb,[switch]$list,$pattern,[switch]$short,[switch]$alias)
if ($list) {dir function:\*-slack* | select name | sort name }
elseif ($alias) {gcm -Module ps.slack | %{gal -Definition $_.name -ea 0}}
elseif (!$znoun -and $pattern -and $short) {gskh | %{foreach ($i in $_) {$i | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}}}
elseif (!$znoun -and $pattern -and !$short) {gskh | out-string | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}
elseif ($znoun -and $pattern -and !$short) {gskh $znoun | out-string | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}
elseif ($znoun -and $pattern -and $short) {gskh $znoun | %{foreach ($i in $_) {$i | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}}}
elseif ($zverb -and !$znoun) {dir function:\$zverb-slack* | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
elseif ($znoun -and !$zverb) {dir function:\*slack$znoun | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
elseif ($zverb -and $znoun) {dir function:\$zverb-slack$znoun | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
else {dir function:\*slack* | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
}
function Remove-EmptyLines {
<#
.Synopsis
Remove empty lines from file, string or variable
.Description
Remove empty lines from file, string or variable
.Example
Remove-EmptyLines -in (gc c:\file.txt)
.Example
$var | Remove-EmptyLines
.Example
help -ex Remove-EmptyLines | Remove-EmptyLines
.Example
gc c:\*.txt | rmel
.Example
Get-ClipBoard | rmel
.Example
dir | oss | rmel
#>
[cmdletbinding()]
[Alias("rmel")]
param ([Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$true)][array]$in)
process {
if (!$psboundparameters.count) {
help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines
return
}
$in.split("`r`n") | ? {$_.trim() -ne ""}
}
}
function Set-slackAuthToken {
<#
.Synopsis
Set slack authentication token
.Description
Set slack authentication token
.Example
Set-slackAuthToken
Set slack authentication token
#>
[CmdletBinding()]
param(
[switch]$force,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)][string]$global:slackToken,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)][string]$global:slackTokenIdentity
)
if (!$global:slackToken) {$global:slackToken=read-host "Input the slack user authentication token"; write-verbose "Slack user token: $global:slackToken"}
else {write-host "`nSlack user token already exists." -f green; write-host "Want to set new one, use -force`n" -f yellow; write-verbose "Slack user token: $global:slackToken"}
if (!$global:slackTokenIdentity) {$global:slackTokenIdentity=read-host "Input the slack identity scope authentication token"; write-verbose "Slack user token: $global:slackTokenIdentity"}
else {write-host "`nSlack identity scope token already exists." -f green; write-host "Want to set new one, use -force`n" -f yellow; write-verbose "Slack user token: $global:slackTokenIdentity"}
# elseif (!$force) {write-host "`nSlack user token already exists." -f green; write-host "Want to set new one, use -force`n" -f yellow; write-verbose "Slack user token: $global:slackToken"}
if ($force) {
if ($global:slackToken) {$global:slackToken=read-host "Input the slack user authentication token"; write-verbose "Slack user token: $global:slackToken"}
if ($global:slackTokenIdentity) {$global:slackTokenIdentity=read-host "Input the slack identity scope authentication token"; write-verbose "Slack user token: $global:slackTokenIdentity"}
}
}
function Test-slackAuthToken {
<#
.Synopsis
Test slack authentication token with slack API endpoint
.Description
Test slack authentication token with slack API endpoint
.Example
Test-slackAuthToken
Test slack token
#>
[CmdletBinding()]
[Alias("tskauth","shskauth")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][ValidateSet('user','identity','bot')][string]$scope="user",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="auth.test"
)
if (!$psboundparameters.count -and !$global:slackToken) {write-host "`nSlack authentication token is not set!`n" -f red; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
switch -wildcard ($scope) {
user {$token=$global:slackToken}
bot {$token=$global:slackToken}
identity {$token=$global:slackTokenIdentity}
}
$Body = @{
token = $token
}
write-verbose "Slack user token: $global:slackToken"
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
function Show-slackAuthToken {
<#
.Synopsis
Show slack authentication token
.Description
Show slack authentication token
.Example
Show-slackAuthToken
Show slack authentication token
#>
[CmdletBinding()]
[Alias("testskconn")]
Param ()
if ($global:slackToken -or $global:slackTokenIdentity) {
"token user scopes: $global:slackToken"
"token identity scopes: $global:slackTokenIdentity"
}
else {
write-host "`nSlack authentication tokens are not set!`n" -f red; Set-slackAuthToken
}
}
function Revoke-slackAuthToken {
<#
.Synopsis
Revoke slack authentication token from Slack API service. New token should be generated
.Description
Revoke slack authentication token from Slack API service. New token should be generated
.Example
Revoke-slackAuthToken
Revoke slack authentication token from Slack API service. New token should be generated
#>
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
[Alias("Revoke-slackAuthToken")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="auth.revoke"
)
if (Test-slackAuthToken) {
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
}
write-verbose ($body | ConvertTo-Json)
if ([bool]$WhatIfPreference.IsPresent) {}
if ($PSCmdlet.ShouldProcess($token,"Revoke the authentication token, i.e. delete from Slack service! You'll need to generate the new token then.)")) {
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$global:slackToken=""
}
$a
}
}
function Test-slackAPI {
<#
.Synopsis
Test slack API
.Description
Test slack API
.Example
Test-slackAPI
Test slack API
#>
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="api.test"
)
if (Test-slackAuthToken) {
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
}
function Disconnect-slack {
<#
.Synopsis
Disconnect from slack, unset the local authentication token
.Description
Disconnect from slack, unset the local authentication token
.Example
Disconnect-slack
Disconnect from slack, unset the local authentication token
#>
[CmdletBinding()]
Param ()
if ($global:slackToken) {$global:slackToken=""}
}
function Send-slackWebhook {
<#
.Synopsis
Post messages to slack via webhook
.Description
Post messages to slack via webhook
.Example
Send-Webhook -url 'https://hooks.slack.com/services/...' -channel '#test' -text "http://www.emoji-cheat-sheet.com/"
Post URL
.Example
Send-Webhook -url 'https://hooks.slack.com/services/...' -channel '@username' -text "This is my hook" -emoji ":yum:" -username myBot
Post message
.Example
"NO", "YES" | %{send-webhook -text $_ -channel "#test" -URL "https://hooks.slack.com/services/..."}
Post two messages
.Example
Send-Webhook -url 'https://hooks.slack.com/services/...' -channel '#smtp' -text ((Invoke-SSHCommand -ComputerName 10.10.20.10 -ScriptBlock {tail -n500 /var/log/maillog | egrep -i "sent"} -Credential $cred) | select -ExpandProperty result) -emoji ":exclamation:" -username myBot
Post from Postfix's maillog
.Example
if ((($body=(Invoke-SSHCommand -ComputerName SMTPserver -ScriptBlock {tail -n100 /var/log/maillog | egrep -i "error|deferred|bounced|blocked"} -Credential $cred) | select -ExpandProperty result))) {send-webhook -url 'https://hooks.slack.com/services/...' -channel '#smtperrors' -text $body -emoji ":exclamation:" -username myBot} else {write-host "$(get-date -f o)`: SMTP is OK" -f green}
Post smtp errors grepped from Postfix email server logs
.Example
(1..1000) | %{if ((($body=(Invoke-SSHCommand -ComputerName 10.10.20.10 -ScriptBlock {tail -n200 /var/log/maillog | egrep -i "error|deferred|bounced|blocked"} -Credential $cred) | select -ExpandProperty result))) {send-webhook -url 'https://hooks.slack.com/services/...' -channel '#smtp' -text $body -emoji ":exclamation:" -username myBot; sleep (10*60)} else {write-host "$_ - $(get-date -f o)`: SMTP is OK" -f green; sleep (30*60)}}
Post smtp errors grepped from Postfix email server logs, if error will occur
#>
[CmdletBinding()]
[Alias("Send-Webhook","sskweb")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$text,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$channel,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$username="myWebhookBot",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$emoji=":exclamation:"
)
process {
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
if ($text.contains("http")) {$text="<$text>"}
$Body = @{
channel = $channel
username = $username
text = $text
icon_emoji = $emoji
unfurl_links = "true"
}
write-verbose ($body | ConvertTo-Json)
$BodyJSON = ConvertTo-Json $Body
$a = Invoke-RestMethod "$URL" -ContentType "application/x-www-form-urlencoded" -Body $BodyJSON -Method Post
$a
}
}
function Send-slackMessage {
<#
.Synopsis
Post messages to slack
.Description
Post messages to slack
.Example
Send-slackMessage -text "Post text to slack" -channel "#test" -Verbose
Post slack message
.Example
sskmsg -channel "#test" -text "Hello world"
Post message
.Example
sskmsg -channel "@name.lastname" -text "Hello!"
Post message to user
.Example
Get-slackUsers | ? presence -match active | select id,name,status,real_name,is_admin,is_bot,presence | ? name -match name | sskmsg -text "Hello"
Post message to user
.Example
Get-slackChannels | ? name -match test | sskmsg -text "Hello world"
Post message
.Example
sskmsg -channel "#BlackFriday" -text (gc c:\errors.log | out-string)
Post message
.Example
Send-slackMessage -text "Alert!" -channel "#test" -as_user $false -emoji ":yum:" -username myBot
Send message as a bot with custom emoji
.Example
Get-Clipboard | out-string | Send-slackMessage -channel "#test"
Paste and send the message
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match (Get-slackChannels | ? name -eq channelSource).id | Send-slackMessage -channel "#channelDestination"
Copy messages from channel to channel
.Example
Get-slackIM | select id,@{n="user";e={(gskusrs | ? id -eq $_.user).name}} | ? user -match user | Send-slackMessage -text "Direct IM message"
Send direct IM message
.Example
(get-vm | ? powerstate -match on | ? name -match centos | get-vmguest | select @{n="IPAddress";e={$_.IPAddress -like "10.10.20.*"}}).ipaddress | new-sshsession
Get-SshSession | Invoke-SshCommand -command {hostname; df -h} | oss | Send-slackMessage -channel "#test"
1. Connect to multiple Linux boxes, using VMware PowerCLI and SSH module
2. Get disk information from all machines and post to slack
3. Every machine info will be posted as separate message
.Example
(get-vm | ? powerstate -match on | ? name -match centos | get-vmguest | select @{n="IPAddress";e={$_.IPAddress -like "10.10.20.*"}}).ipaddress | new-sshsession
Get-SshSession | Invoke-SshCommand -command {hostname; df -h} | out-string | Send-slackMessage -channel "#test"
Same as previous example, but all text will be posted as single message
.Example
gskusrs | ? name -match name | startskim | sskmsg -text "text"
Send private message
.Example
Get-ZabbixAlert | ? sendto -match yubu | select @{n="Time(UTC+1)";e={(convertfrom-epoch $_.clock).addhours(1)}},alertid,subject | Out-String | sskmsg -channel "#BlackFriday"
Get Zabbix alerts for last 5 hours (default) and post to slack
#>
[CmdletBinding()]
[Alias("sskmsg")]
Param (
[Alias("id")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$channel,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)][string]$text,
# Set your bot's user name. Must be used in conjunction with as_user set to false, otherwise ignored.
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$username,
# Emoji to use as the icon for this message. Overrides icon_url. Must be used in conjunction with as_user set to false, otherwise ignored
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$emoji=":exclamation:",
# Pass true to post the message as the authed user, instead of as a bot
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$as_user=$true,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$parse="full",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$attachments,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$unfurl_links=$true,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$unfurl_media=$true,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="chat.postMessage"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $channel
username = $username
text = $text
icon_emoji = $emoji
unfurl_links = $unfurl_links
unfurl_media = $unfurl_media
as_user = $as_user
parse = $parse
attachments = $attachments
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
}
function Send-slackMessageAsBot {
<#
.Synopsis
Post messages as bot
.Description
Post messages as a bot. All users in the chanel will get notification
.Example
Send-slackMessageAsBot -text "Post text to slack" -channel "#test" -username alertBot -emoji ":thumbsup:"
Post slack message as a bot, named alertBot
.Example
sskmsgab -channel "#BlackFriday" -text (gc c:\errors.log | out-string) -username alertBot -emoji ":exclamation:"
Post message as a bot
.Example
Get-Clipboard | out-string | Send-slackMessageAsBot -channel "#test" -username systemAlertBot
Paste and send the message as a bot, named systemAlertBot
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match (Get-slackChannels | ? name -eq channelSource).id | Send-slackMessageAsBot -channel "#channelDestination" -emoji :exclamation: -as_user $false
Copy messages from channel to channel
.Example
(get-vm | ? powerstate -match on | ? name -match centos | get-vmguest | select @{n="IPAddress";e={$_.IPAddress -like "10.10.20.*"}}).ipaddress | new-sshsession
Get-SshSession | Invoke-SshCommand -command {hostname; df -h} | oss | Send-slackMessageAsBot -channel "#alerts" alertBot -emoji ":exclamation:"
1. Connect to multiple Linux boxes, using VMware PowerCLI and SSH module
2. Get disk information from all machines and post to slack
3. Every machine info will be posted as separate message
.Example
(get-vm | ? powerstate -match on | ? name -match centos | get-vmguest | select @{n="IPAddress";e={$_.IPAddress -like "10.10.20.*"}}).ipaddress | new-sshsession
Get-SshSession | Invoke-SshCommand -command {hostname; dstat -lvn 1 2} | out-string | Send-slackMessageAsBot -channel "#BlackFriday" -user alertBot -emoji ":exclamation:"
Same as previous example, but all text will be posted as single message
.Example
Get-ZabbixAlert | ? sendto -match user | select @{n="Time(UTC+1)";e={(convertfrom-epoch $_.clock).addhours(1)}},alertid,subject | Out-String | sskmsgab -channel "#BlackFriday" alertBot -emoji ":boom:"
Get Zabbix alerts for last 5 hours (default) and post to slack
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match (Get-slackChannels | ? name -eq channelsource).id | Send-slackMessage -channel "#channeldest" -emoji :exclamation: -as_user $false -text {(($_.text).replace('<','').replace('>','')).split('|')[0]}
Copy messages with URLs from channel to channel
#>
[CmdletBinding()]
[Alias("sskmsgab")]
Param (
[Alias("id")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$channel,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)][string]$text,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$username,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$emoji,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$parse="full",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$attachments,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="chat.postMessage"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $channel
username = $username
text = $text
icon_emoji = $emoji
unfurl_links = "true"
unfurl_media = "true"
as_user = "false"
parse = "true"
attachments = $attachments
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
}
function Set-slackMessage {
<#
.Synopsis
Edit slack messages
.Description
Edit slack messages
.Example
Search-slack -query "query" | select -ExpandProperty messages | select -ExpandProperty matches | Set-slackMessage -text "New message here" -channel {$_.channel.id}
Will replace existing message text with new one, in every occurrence
.Example
Search-slack -query "query" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match test | Set-slackMessage -text "EDIT MESSAGE" -channel {$_.channel.id}
Will replace messages by query and in channel by match
.Example
Search-slack -query "query" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match test | select -skip 1 | Set-slackMessage -text {"$($_.text) Additional text"} -channel {$_.channel.id}
Will edit message by appending new text to existing one
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match (Get-slackChannels | ? name -eq powershell).id | sort ts -desc | ? text -match ":boy:" | Set-slackMessage -text {($_.text).replace(':boy:','')} -channel {$_.channel.id}
Will remove ghost emoji from the message(s)
.Example
Get-slackChannels | ? name -match powershell -pv aa | Get-slackChannelsHistory | ? text -match ":boy:" | Set-slackMessage -text {($_.text).replace(':boy:','')} -channel {$aa.id}
Will remove ghost emoji from the message(s)
#>
[CmdletBinding()]
[Alias("Edit-slackMessage","editskmsg")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$channel,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$ts,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$parse="full",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$link_names=1,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$text,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$as_user="true",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="chat.update"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
# "channel": "@username".
token = $token
channel = $channel
ts = $ts
text = $text
parse = $parse
link_numbers = $link_numbers
as_user = $as_user
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
}
function Remove-slackMessage {
<#
.Synopsis
Delete messages from slack
.Description
Delete messages from slack
.Example
Get-slackChannels | ? name -match channelName -pv aa | gskchhist | select ts,@{n='id';e={$aa.id}} | rmskmsg
Get-slackChannels | ? name -match channelName -pv aa | gskchhist | select ts,@{n='channel';e={$aa.id}} | rmskmsg
Delete all messages from the channel
.Example
Get-slackChannels | ? name -match channelName -pv aa | gskchhist | ? text -match "text" | select ts,@{n='channel';e={$aa.id}} | rmskmsg
Delete message, by text match
.Example
Get-slackChannels | ? name -match test | Get-slackChannelsHistory | ? text -match hello | rmskmsg -id (Get-slackChannels | ? name -match test).id
Delete messages from the channel by text match
.Example
Get-slackChannels | ? name -match test | delskmsg -ts (Get-slackChannels | ? name -match test | gskchhist | ? text -match "hello" | select -first 1).ts
Delete messages from channel #test. Will delete only one message
.Example
Get-slackChannels | ? name -match powershell -pv aa | gskchhist | ? ts -gt "$(get-date -date '2/25/2019 12:00' | convertto-epoch)"| select ts,@{n='id';e={$aa.id}},text | sort ts -desc | rmskmsg
Delete messages newer than 2/25/2019 12:00 (UTC time)
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match (Get-slackChannels | ? name -eq powershell).id | sort ts -desc | select -first 8 | delskmsg -channel {$_.channel.id}
Delete messages
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match (Get-slackChannels | ? name -eq powershell).id | sort ts -desc | select -skiplast 2 | select ts,@{n='channel';e={$_.channel.id}} | delskmsg
Delete messages
#>
[CmdletBinding()]
[Alias("Delete-slackMessage","delskmsg","rmskmsg")]
Param (
[Alias('channel')][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$ts,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="chat.delete"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $id
ts = $ts
as_user = "true"
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
}
function Get-slackEmoji {
<#
.Synopsis
Get emoji list from slack
.Description
Get emoji list from slack
.Example
gskemoji
Get slack emoji list
.Example
chrome --incognito http://www.emoji-cheat-sheet.com/
chrome --incognito http://www.webpagefx.com/tools/emoji-cheat-sheet/
Get emoji cheat sheet
#>
[CmdletBinding()]
[Alias("gskemoji")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="emoji.list"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a.emoji
}
}
function Search-slack {
<#
.Synopsis
Search slack messages and files
.Description
Search slack messages and files
.Example
Search-slack -query "powershell"
Search slack
.Example
Search-slack -query "powershell" | select @{n="MsgTotal"; e={$_.messages.total}}, @{n="FilesTotal";e={$_.files.total}},@{n="PostsTotal";e={$_.posts.total}}
Get total messages, files and posts for query
.Example
Search-slack -query "powershell" | select -ExpandProperty files | select -ExpandProperty matches | ft -a
Search slack files
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ft -a
Search slack messages
.Example
Search-slack -query "powershell" | select -ExpandProperty files | select -ExpandProperty matches | select @{n="created";e={convertfrom-epoch $_.created}},name,title,url_private_download
Search slack files
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | select text,@{n='channel';e={(gskch | ? id -Match $_.channel.id).name}} | fl *
search slack messages
.Example
Search-slack -query "powershell" | select -ExpandProperty posts | select -ExpandProperty matches | ft -a
Search posts
.Example
Search-slack -query "powershell" | select -ExpandProperty messages | select -ExpandProperty matches | ? channel -match test | select text,@{n="PrevText";e={$_.previous.text}} | fl *
View edited messages previous version, if exist
#>
[CmdletBinding()]
[Alias("ssk")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$query,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$sort_dir,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$count="1000",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="search.all"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
query = $query
sort = "timestamp"
sort_dir = $sort_dir
count = $count
highlight = "1"
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
$a
}
}
function Get-slackPins {
<#
.Synopsis
Get pinned items for channel
.Description
Get pinned items for channel
.Example
Get-slackChannels | ? name -match "" | Get-slackPins
Get pinned messages for channels
.Example
gskch | ? name -match "" | gskpins | select @{n="channel";e={(gskch | ? id -Match $_.channel).name}} -ExpandProperty message
Get pinned messages for channels
#>
[CmdletBinding()]
[Alias("gskpins")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$ID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="pins.list"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $ID
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
if ($a.ok) {$a.items} else {$a}
}
}
function Set-slackPins {
<#
.Synopsis
Pin item to channel
.Description
Pin item to channel
.Example
Search-slack -query "Some text" | select -ExpandProperty messages | select -ExpandProperty matches | select -first 1 | Set-slackPins -channel {$_.channel.id}
Pin message to channel
.Example
gskch | ? name -match "" | gskpins | select @{n="channel";e={(gskch | ? id -Match $_.channel).name}} -ExpandProperty message
Get pinned messages for channels
#>
[CmdletBinding()]
[Alias("sskpins")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$channel,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$file,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$ts,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="pins.add"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $channel
file = $file
timestamp = $ts
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
if ($a.ok) {$a.items} else {$a}
}
}
function Remove-slackPins {
<#
.Synopsis
Remove pin from item
.Description
Remove pin from item
.Example
Get-slackChannels | ? name -match "" | Get-slackPins | select channel -ExpandProperty message | Remove-slackPins
Unpin message
#>
[CmdletBinding()]
[Alias("Delete-slackPins")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$channel,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$file,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$ts,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="pins.remove"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $channel
file = $file
timestamp = $ts
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
if ($a.ok) {$a} else {$a}
}
}
function Get-slackChannels {
<#
.Synopsis
Get slack channels
.Description
Get slack channels
.Parameter exclude_archived
exclude_archived=1 is default, Set to 0, to include also archived in list
.Example
Get-slackChannels | select id,name,num_members | sort num_members -desc
Get list of channels and their members count
.Example
gskch | select id,name,@{n="created";e={(convertFrom-epoch $_.created).addhours(-5)}},@{n="creator";e={(gskusrs | ? id -eq $_.creator).name}},num_members | sort created -desc | ft -a
Get list of channels, display time in UTC-5
.Example
gskch -exclude_archived 0 | ? is_archived | select id,name,@{n="created";e={(convertFrom-epoch $_.created).addhours(-5)}}
Get archived channels
.Example
Get-slackChannel | ? name -match chanelName
Get channels by name match
.Example
gskch | select id,name,num_members
Get channels
.Example
gskch | ? name -match channelName | gskpins
Get pinned messages for channel
#>
[CmdletBinding()]
[Alias("gskch")]
Param (
$exclude_archived=1,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="channels.list"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
exclude_archived = $exclude_archived
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
if ($a.ok) {$a.channels} else {$a}
}
}
function Get-slackChannelsInfo {
<#
.Synopsis
Get slack channel info
.Description
Get slack channel info
.Example
Get-slackChannels | ? name -match "" | Get-slackChannelsInfo
Get channel info
.Example
Get-slackChannels | ? name -match "" | Get-slackChannelsInfo | select id,name -ExpandProperty latest -ea silent
Get latest post in channel
.Example
Get-slackChannels | ? name -match "channelName" | Get-slackChannelsInfo | select -ExpandProperty members | select @{n="Members";e={(gskusrs | ? id -match $_)}} | select -ExpandProperty members | select id,team_id,name,real_name,presence,is_admin,is_owner | sort name | ft -a
Get channel members
.Example
Get-slackChannels | ? name -match "" | Get-slackChannelsInfo | select id,name,@{n='creator';e={(gskusrs | ? id -match $_.creator).name}},@{n="created";e={convertfrom-epoch $_.created}} | sort creator | ft -a
Get channels creation info
#>
[CmdletBinding()]
[Alias("gskchi")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$token=$global:slackToken,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL="https://slack.com/api",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$method="channels.info"
)
begin {if (!(Test-slackAuthToken)) {break}}
process {
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
token = $token
channel = $id
}
write-verbose ($body | ConvertTo-Json)
$a = Invoke-RestMethod "$URL/$method" -Body $Body -Method Post
if ($a.ok) {$a.channel} else {$a}
}
}
function New-slackChannels {
<#
.Synopsis
Join slack channel. If the channel does not exist, it will be created
.Description