forked from gildas/posh-ic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Get-ICUsersLegacy.ps1
120 lines (101 loc) · 4.24 KB
/
Get-ICUsersLegacy.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
<#
# AUTHOR : Pierrick Lozach, extended by Paul McGurn
#>
function Get-ICUsersLegacy() {
<#
.SYNOPSIS
Gets a list of all users using legacy PowerShell Invoke-WebRequest instead of Invoke-RestMethod.
.DESCRIPTION
Gets a list of all users using legacy PowerShell Invoke-WebRequest instead of Invoke-RestMethod
Invoke-RestMethod doesn't have good backwards compatibilitly with older versions of PowerShell.
.PARAMETER ICSession
The Interaction Center Session
#> # }}}3
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)] [Alias("Session", "Id")] $ICSession,
[Parameter(Mandatory = $false)] [Alias("Fields", "Columns")] $Properties
)
$headers = @{
"Accept-Language" = $ICSession.language;
"ININ-ICWS-CSRF-Token" = $ICSession.token;
}
#default URI to pull just base User objects for performance
$uri = "$($ICsession.baseURL)/$($ICSession.id)/configuration/users"
#we'll use the supplied properties (can be "*") to get detailed results, if the param was supplied
if (![String]::IsNullOrEmpty($properties)) {
Write-Verbose "Called with specific properties, will use 'select' querystring"
$uri = "$($ICsession.baseURL)/$($ICSession.id)/configuration/users?select=${properties}"
}
$response = Invoke-WebRequest -Uri $uri `
-Method Get `
-Headers $headers `
-WebSession $ICSession.webSession `
-ErrorAction Stop
$headers = $response.headers
$response = ConvertFrom-Json $response
$firstsize = $response.items.count
Write-Verbose "First API call result size: ${firstsize}"
$contentrange = $headers.'Content-Range'
$results = [System.Collections.ArrayList]@()
foreach ($i in $response.items) {
$results.add($i)>$null
}
$pcr = parseContentRange($contentrange)
if ($pcr.total -gt 200) {
Write-Verbose "Submitting API calls for additional users"
#we have more API calls to submit
foreach ($r in $pcr.AdditionalRanges) {
Write-Verbose "Getting Users in range: ${r}"
$additionalrequestheaders = @{
"Accept-Language" = $ICSession.language;
"ININ-ICWS-CSRF-Token" = $ICSession.token;
"Range" = "items=" + $r;
}
$headerstring = $additionalrequestheaders | ConvertTo-Json -Depth 4
Write-Verbose "Request Headers: ${headerstring}"
$additionalresponse = Invoke-Webrequest -Uri $uri `
-Method Get `
-Headers $additionalrequestheaders `
-WebSession $ICSession.webSession `
-ErrorAction Stop
$headers = $additionalresponse.headers
$additionalresponse = ConvertFrom-Json $additionalresponse
$size = $additionalresponse.items.count
Write-Verbose "Size for ${r} is ${size}"
$responseheaders = $headers | ConvertTo-Json -Depth 8
Write-Verbose "Response headers: ${responseheaders}"
foreach ($i in $additionalresponse.items) {
$results.add($i)>$null
}
}
}
return $results
}
function parseContentRange($contentrange) {
#ex. items 1-199/1050
$range = $contentrange.split(" ")[1]
$total = [Int32]$range.split("/")[1]
Write-Verbose "Total count of users: ${total}"
$wholesets = [Math]::Floor($total / 200)
$lastrangestart = $wholesets * 200
$result = [PSCustomObject]@{
Total = $total
}
if ($total -gt 200) {
Write-Verbose "Ranges are greater than 200"
$ranges = [System.Collections.ArrayList]@()
#start with second range, as the initial API call will get the first
for ($i = 1; $i -lt $wholesets; $i++) {
$rangestart = $i * 200
$rangeend = $rangestart + 199
$rangestring = "${rangestart}-${rangeend}"
$ranges.add($rangestring)>$null
}
#add the last partial range
$lastrange = "${lastrangestart}-${total}"
$ranges.add($lastrange)>$null
$result | Add-Member -NotePropertyName AdditionalRanges -NotePropertyValue $ranges
}
return $result
}