private/utility/Get-ConfluenceGroupsUsers.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
function Get-ConfluenceGroupsUsers {
    [CmdletBinding()]
    param (
        # the name of the group to get users for
        [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [Alias("Group_Name")]
        [string]
        $Group
    )
    
    begin {
        $users = @()
    }
    
    process {
        #set up to loop through results (there is a limit of 200 per call, so we have to loop to make sure we got them all)
        $limit = 200
        $currentPage = 1
        $returnCount = 0
        do {
            Write-Verbose "Fetching Page $currentPage of users for $Group"
            #get a response
            $results = Invoke-ConfluenceGetGroupMembers $Group -MaxResults $limit -StartAt ($limit * ($currentPage - 1))

            #map users
            $mappedUsers = $results.results | Read-ConfluenceUser -RefreshId $RefreshId

            #add to list, checking for duplicates
            $existingIds = @()
            $existingIds += $users | ForEach-Object { $_.Account_Id }
            $users += $mappedUsers | Where-Object { $existingIds -notcontains $_ }
            
            #check how many results came back
            $returnCount = $results.size

            #move the start marker forward
            $currentPage += 1

            #loop again if we got the max number of results (implying that we haven't go them all yet)
        } while ($returnCount -eq $limit)
    }
    
    end {
        $users
    }
}