Mattermost.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
function New-MMToken
{
   <#
       .SYNOPSIS
       Creates token for MatterMost
       .DESCRIPTION
       This function returns token for MatterMost. For posting a message, first you need to create a token on MatterMost.
       .EXAMPLE
       New-MatterMostToken -url "http://matterdns:8065/api/v4/users/login" -MatterUser "user1" -MatterPass "password123"
       .PARAMETER url
       Specify the login url of MatterMost API
       .PARAMETER MatterUser
       Specify the user name for MatterMost
       .PARAMETER MatterPass
       Specify the password for MatterUser
   #>

   [CmdletBinding()]
   Param (
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost URL')]
           [ValidatePattern('https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.([-a-zA-Z0-9@:%_\+.~#?&//=]*)')]
       [string]$url,         
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost UserName')]
       [string]$MatterUser,
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost Password')]
       [string]$MatterPass     

       )
   Write-Verbose -Message "Creating json for user $MatterUser"
   $json = @{
           "login_id"="$MatterUser"
           "password"="$MatterPass"
       } | ConvertTo-Json
   Write-Verbose -Message "Creating token for MatterMost ($url) using json"
   $token = (Invoke-WebRequest -Method Post -Uri $url -Body $json -ContentType 'application/json' -UseBasicParsing).Headers.Token
   return $token
}

function New-MMPost
{
    <#
       .SYNOPSIS
       Posts message to MatterMost channel
       .DESCRIPTION
       This function Posts message to MatterMost channel. For posting a message, first you need to create a token on MattrerMost.
       .EXAMPLE
       New-MMPost -url "http://matterdns:8065/api/v4/posts" -MatterToken $token -ChannelID "r8jbmbjsjbyiiqtbj4n4nqg7jh" -Message "this is a sample message"
       .PARAMETER url
       Specify the post url of MatterMost API
       .PARAMETER MatterToken
       Specify the token object for MatterMost
       .PARAMETER ChannelID
       Specify the channel id that you want to send message
       .PARAMETER Message
       Specify the message that you want to send
   #>

   [CmdletBinding()]
   Param (
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost URL')]
           [ValidatePattern('https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.([-a-zA-Z0-9@:%_\+.~#?&//=]*)')]
       [string]$url,         
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost UserName')]
       [string]$ChannelID,
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost Password')]
       [string]$Message,     
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='Please specify a valid token for MatterMost API')]
       $MatterToken
       )
   Write-Verbose -Message "Creating header for Authorization"
   $header = @{
       "Authorization" = "Bearer $token"
       }
   Write-Verbose -Message "Created header:`n$($header | Out-String)"
   Write-Verbose -Message "Creating body"
   $body = @{
           "channel_id"= $ChannelID
           "message"= $Message
       } | ConvertTo-Json
   Write-Verbose -Message "Json formatted body created:`n$body"
   Invoke-RestMethod -Method Post -Uri $url -Headers $header -ContentType "text/plain; charset=utf-8" -Body $body

}


function Get-MMProperties
{
    <#
       .SYNOPSIS
       Gets properties from MatterMost
       .DESCRIPTION
       This function gets properties from MatterMost(Ex:teams). For getting a property, first you need to create a token on MattrerMost.
       .EXAMPLE
       New-MMPost -url "http://matterdns:8065/api/v4" -MatterToken $token -MMProperty "teams"
       .PARAMETER url
       Specify the post url of MatterMost API
       .PARAMETER MatterToken
       Specify the token object for MatterMost
       .PARAMETER Property
       Specify the property that you want to see
   #>

   [CmdletBinding()]
   Param (
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost URL')]
           [ValidatePattern('https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.([-a-zA-Z0-9@:%_\+.~#?&//=]*)')]
           [string]$url,         
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='MatterMost property')]
           [string]$MMProperty,
          
       [Parameter(Mandatory=$True,
           ValueFromPipeline=$True,
           ValueFromPipelineByPropertyName=$True,
           HelpMessage='Please specify a valid token for MatterMost API')]
           $MatterToken
       )
   Write-Verbose -Message "Getting '$MMProperty' property from MatterMost"
   $url = $url + $MMProperty
   Write-Verbose -Message "MatterMost URL :$url"
   Write-Verbose -Message "Creating header for Authorization"
   $header = @{
       "Authorization" = "Bearer $token"
       }
   Write-Verbose -Message "Created header:`n$($header | Out-String)"
   Invoke-RestMethod -Method get -Uri $url -Headers $header
}