-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-TeamsStatus.ps1
270 lines (231 loc) · 11.1 KB
/
Get-TeamsStatus.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
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
<#
.NOTES
Name: Get-TeamsStatus.ps1
Author: Danny de Vries
Requires: PowerShell v2 or higher
Version History: https://github.com/EBOOZ/TeamsStatus/commits/main
.SYNOPSIS
Sets the status of the Microsoft Teams client to Home Assistant.
.DESCRIPTION
This script is monitoring the Teams client logfile for certain changes. It
makes use of two sensors that are created in Home Assistant up front.
The status entity (sensor.teams_status by default) displays that availability
status of your Teams client based on the icon overlay in the taskbar on Windows.
The activity entity (sensor.teams_activity by default) shows if you
are in a call or not based on the App updates deamon, which is paused as soon as
you join a call.
.PARAMETER SetStatus
Run the script with the SetStatus-parameter to set the status of Microsoft Teams
directly from the commandline.
.EXAMPLE
.\Get-TeamsStatus.ps1 -SetStatus "Offline"
#>
# Configuring parameter for interactive run
Param(
$SetStatus,
$debugEnabled = $false
)
# Set language variables below
$lgAvailable = "Available"
$lgBusy = "Busy"
$lgOnThePhone = "On the phone"
$lgAway = "Away"
$lgBeRightBack = "Be right back"
$lgDoNotDisturb = "Do not disturb"
$lgPresenting = "Presenting"
$lgFocusing = "Focusing"
$lgInAMeeting = "In a meeting"
$lgOffline = "Offline"
$lgNotInACall = "Not in a call"
$lgInACall = "In a call"
# Choose logging style, either eventlog or file
$logType = "eventlog"
# Config
$teamsLogFile = "C:\Users\Damien\AppData\Roaming\Microsoft\Teams\logs.txt"
$webhookUrl = "http://192.168.1.34:8000/webhook"
$pollFrequency = 5
$logFilePath = "Get-TeamsStatus.log"
function Write-LogEvent {
param (
$logType,
$logMessage,
$enableStdout = $false
)
if ($logType -eq "eventlog") {
Write-EventLog -LogName "Application" -Source "TeamsStatus" -EventID 3001 -EntryType Information -Message $logMessage -Category 1 -RawData 10,20
}
if ($logType -eq "file") {
Add-Content $logFilePath "$(Get-Date) - Status: $logMessage"
}
if ($enableStdout -eq $true){
Write-Host "$(Get-Date) - $logMessage"
}
}
function Invoke-PythonWebHook {
param (
$color
)
$params = @{
"color"="$color";
}
$params = $params | ConvertTo-Json
$result = try {
(Invoke-RestMethod -Uri $webhookUrl -Method POST -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json" -ErrorAction Stop).BaseResponse
} catch [System.Net.WebException] {
$result_error = $($_.Exception.Message)
Write-LogEvent -logtype $logType -enableStdout $true -logMessage "Teams Status webhook call failed result: $result_error"
}
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Wehook $webhookUrl called" }
}
# Run the script when a parameter is used and stop when done
If($null -ne $SetStatus){
Write-Host ("Setting Microsoft Teams light to " + $SetStatus )
$params = @{
"color"="$SetStatus";
}
$params = $params | ConvertTo-Json
$result = Invoke-RestMethod -Uri $webhookUrl -Method POST -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json"
break
}
# Start monitoring the Teams logfile when no parameter is used to run the script
Write-LogEvent -logtype $logType -enableStdout $true -logMessage "Teams Status started"
Write-LogEvent -logtype $logType -enableStdout $true -logMessage "Processing Teams log file: $env:APPDATA\Microsoft\Teams\logs.txt"
$Enable = 1
DO {
# Get Teams Logfile and last icon overlay status
$TeamsStatus = Get-Content -Path $teamsLogFile -Tail 1000 | Select-String -Pattern `
'Setting the taskbar overlay icon -',`
'StatusIndicatorStateService: Added' | Select-Object -Last 1
# Get Teams Logfile and last app update deamon status
$TeamsActivity = Get-Content -Path $teamsLogFile -Tail 1000 | Select-String -Pattern `
'Resuming daemon App updates',`
'Pausing daemon App updates',`
'SfB:TeamsNoCall',`
'SfB:TeamsPendingCall',`
'SfB:TeamsActiveCall',`
'name: desktop_call_state_change_send, isOngoing' | Select-Object -Last 1
# Get Teams application process
$TeamsProcess = Get-Process -Name Teams -ErrorAction SilentlyContinue
# Check if Teams is running and start monitoring the log if it is
If ($null -ne $TeamsProcess) {
If($TeamsStatus -eq $null){ }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgAvailable*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added Available*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Available -> NewActivity*") {
$Status = $lgAvailable
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgBusy*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added Busy*" -or `
$TeamsStatus -like "*Setting the taskbar overlay icon - $lgOnThePhone*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added OnThePhone*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Busy -> NewActivity*") {
$Status = $lgBusy
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status"}
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgAway*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added Away*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Away -> NewActivity*") {
$Status = $lgAway
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgBeRightBack*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added BeRightBack*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: BeRightBack -> NewActivity*") {
$Status = $lgBeRightBack
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgDoNotDisturb *" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added DoNotDisturb*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: DoNotDisturb -> NewActivity*") {
$Status = $lgDoNotDisturb
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgFocusing*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added Focusing*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Focusing -> NewActivity*") {
$Status = $lgFocusing
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgPresenting*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added Presenting*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Presenting -> NewActivity*") {
$Status = $lgPresenting
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgInAMeeting*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added InAMeeting*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: InAMeeting -> NewActivity*") {
$Status = $lgInAMeeting
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgOffline*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added Offline*") {
$Status = $lgOffline
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
}
If($TeamsActivity -eq $null){ }
ElseIf ($TeamsActivity -like "*Resuming daemon App updates*" -or `
$TeamsActivity -like "*SfB:TeamsNoCall*" -or `
$TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: false*") {
$Activity = $lgNotInACall
$ActivityIcon = $iconNotInACall
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Activity: $Activity" }
}
ElseIf ($TeamsActivity -like "*Pausing daemon App updates*" -or `
$TeamsActivity -like "*SfB:TeamsActiveCall*" -or `
$TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: true*") {
$Activity = $lgInACall
$ActivityIcon = $iconInACall
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Activity: $Activity" }
}
}
# Set status to Offline when the Teams application is not running
Else {
$Status = $lgOffline
$Activity = $lgNotInACall
$ActivityIcon = $iconNotInACall
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Status: $Status" }
if ($debugEnabled -eq $true) { Write-Host "$(Get-Date) - Activity: $Activity" }
}
# Call Home Assistant API to set the status and activity sensors
If ($CurrentStatus -ne $Status -and $Status -ne $null) {
$CurrentStatus = $Status
if ($Activity -eq "In a call") {
$color = "red"
}
if ($Activity -eq "Not in a call") {
$color = "green"
}
if ($Status -eq "Do not disturb") {
$color = "red"
}
if ($Status -eq "Away" -or $Status -eq "Offline") {
$color = "off"
}
if ($Status -eq "Available" -and $Activity -eq "Not in a call"){
$color = "green"
}
Invoke-PythonWebHook -color $color
}
If ($CurrentActivity -ne $Activity) {
$CurrentActivity = $Activity
if ($Activity -eq "In a call") {
$color = "red"
}
if ($Activity -eq "Not in a call") {
$color = "green"
}
if ($Status -eq "Do not disturb") {
$color = "red"
}
if ($Status -eq "Available" -and $Activity -eq "Not in a call"){
$color = "green"
}
if ($Status -eq "Away" -or $Status -eq "Offline") {
$color = "off"
}
Invoke-PythonWebHook -color $color
}
Start-Sleep $pollFrequency
} Until ($Enable -eq 0)