private/steps/Update-ConfluenceGroups.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
function Update-ConfluenceGroups {
    [CmdletBinding()]
    param (
        # The ID value of the refresh underway
        [Parameter(Mandatory,Position=0)]
        [int]
        $RefreshId,

        # The sql instance to update data in
        [Parameter(Mandatory,Position=1)]
        [string]
        $SqlInstance,

        # The sql database to update data in
        [Parameter(Mandatory,Position=2)]
        [string]
        $SqlDatabase,

        # The schema to use when updating data
        [Parameter(Position=3)]
        [string]
        $SchemaName="dbo"
    )
    
    begin {
        Write-Verbose "Updating Groups"
        $tableName = "tbl_stg_Confluence_Group"
        $groups = @()

        $sqlConnSplat = @{
            DatabaseServer = $SqlInstance
            DatabaseName = $SqlDatabase
            SchemaName = $SchemaName
        }
    }
    
    process {
        Write-Verbose "Getting Groups"

        #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
        $start = 0
        $returnCount = 0
        do {
            #get a response
            $results = Invoke-ConfluenceGetGroups -MaxResults $limit -StartAt $start

            #map groups
            $mappedGroups = $results.results | Read-ConfluenceGroup -RefreshId $RefreshId

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

            #move the start marker forward
            $start += $limit

            #loop again if we got the max number of results (implying that we haven't go them all yet)
        } while ($returnCount -eq $limit)
    }
    
    end {
        Write-AtlassianData @sqlConnSplat -Data $groups -TableName $tableName
        return $groups
    }
}