-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSet-SfbClientPresence.ps1
101 lines (83 loc) · 3.92 KB
/
Set-SfbClientPresence.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
Function Set-SfbClientPresence {
<#
.Synopsis
Set Skype for Business presence
.Description
This function will call the MakeMeAvailable function in UCWA if it's the first time this function was called within the context. This allows the user to receive messages. Next to that,
the module will start a runspace to post to the reportMyActivity interface of UCWA to keep the user active.
If the user is already available (MakeMeAvailable was called before) then it just changes the presence of the user.
.Example
Set-SfbClientPresence -Availability Online
Set's the user online.
#>
[CmdletBinding()]
Param (
#
[Parameter(Mandatory)]
[ValidateSet('Online','Away','BeRightBack','Busy','DoNotDisturb','Offline')]
$Availability,
# The Skype for Business application context to send the message in.
[Parameter()]
[object]$Context = $SkypeForBusinessClientModuleContext
)
$rest = @{
ContentType = 'application/json'
Headers = $Context.authHeader
}
$makeMeAvailable = $false
$myPresenceUri = '{0}{1}{2}' -f $Context.rootUri, $Context.application._embedded.me._links.self.href, '/presence'
Try {
$myPresence = Invoke-RestMethod -Method Get -Uri $myPresenceUri @rest -ErrorAction stop
}
Catch {
If(($_.ErrorDetails.Message | ConvertFrom-Json).subcode -eq 'MakeMeAvailableRequired') {
$makeMeAvailable = $true
}
}
If($makeMeAvailable) {
$makeMeAvailableUri = '{0}{1}' -f $Context.rootUri, $Context.application._embedded.me._links.makeMeAvailable.href
$makeMeAvailableBody = ConvertTo-Json @{
signInAs = $availability
supportedMessageFormat = 'Plain'
supportedModalities = @('Messaging')
}
Try {
$null = Invoke-RestMethod -Method Post -Uri $makeMeAvailableUri -Body $makeMeAvailableBody @rest -ErrorAction Stop
$reportMyActivityRunspace = [runspacefactory]::CreateRunspace()
$reportMyActivityRunspace.ApartmentState = 'STA'
$reportMyActivityRunspace.ThreadOptions = 'ReuseThread'
$reportMyActivityRunspace.Open()
# Skype Client Specific
$reportMyActivityRunspace.SessionStateProxy.SetVariable('SkypeForBusinessClientModuleContext',$Context)
$psCmd = [PowerShell]::Create().AddScript({
$reportMyActivityUri = '{0}{1}' -f $SkypeForBusinessClientModuleContext.applicationUri, '/reportMyActivity'
While($true) {
Invoke-RestMethod -Uri $reportMyActivityUri -Method Post -Headers $SkypeForBusinessClientModuleContext.authHeader
Start-Sleep -Seconds 120
}
})
$psCmd.Runspace = $reportMyActivityRunspace
$reportMyActivityHandle = $psCmd.BeginInvoke()
$null = [System.Threading.Monitor]::Enter($Context.backgroundProcesses.syncroot)
$null = $Context.backgroundProcesses.Add(
[pscustomobject]@{
Name = "reportMyActivityRunspace"
Handle = $reportMyActivityHandle
Runspace = $psCmd
}
)
$null = [System.Threading.Monitor]::Exit($Context.backgroundProcesses.syncroot)
}
Catch {
Throw "MakeMeAvailable call failed. Error: $_"
}
# Get the updated application resource
$null = Invoke-RestMethod -Method Get -Uri $Context.applicationUri @rest
}
else {
$myPresenceBody = ConvertTo-Json @{
availability = $availability
}
$null = Invoke-RestMethod -Method Post -Uri $myPresenceUri -Body $myPresenceBody @rest
}
}