Functions/Get-SyncMSPCompleteUsersScriptBlocks.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
<#
.SYNOPSIS
    This function returns the script blocks used to sync MSPComplete users.
#>

function Get-SyncMSPCompleteUsersScriptBlocks {
    # Return the script blocks
    return @{
        UnArchiveEntity = {
            # Retrieve the archived user (if it exists)
            $archivedUser = Get-BT_CustomerEndUser -Ticket $mspcObject.CustomerTicket -DisplayName $entity.Expected.DisplayName `
                -IsArchived $true -IsDeleted $false -Environment $environment

            # The archived user doesn't exist
            if (!$archivedUser) {
                return
            }

            # If there are multiple archived users, use the first one
            if ($archivedUser.length -gt 1) {
                $archivedUser = $archivedUser[0]
            }

            # Un-archive the user
            $unArchivedUser = Set-BT_CustomerEndUser -Ticket $mspcObject.CustomerTicket -CustomerEndUser $archivedUser `
                                    -IsArchived $false -Environment $environment
            if (!$unArchivedUser) {
                throw "Failed to un-archive user."
            }
            "success"
        }
        CreateEntity = {
            # Create hash table for params
            $addBTCustomerEndUserParams = @{
                Ticket      = $mspcObject.CustomerTicket
                Environment = $environment
            }

            # Add built-in properties to the params
            Get-MSPCompleteUserPropertyList | ForEach-Object -Process {
                if ($entity.Expected.$_) {
                    $addBTCustomerEndUserParams.Add($_, $entity.Expected.$_)
                }
            }

            # Create the user
            $newUser = Add-BT_CustomerEndUser @addBTCustomerEndUserParams
            if (!$newUser) {
                throw "Failed to create user."
            }

            # Add extended properties to the user
            $extendedPropertyErrors = ""
            $extendedProperties = Get-MSPCompleteUserExtendedPropertyList
            foreach ($property in $extendedProperties) {
                if (![String]::IsNullOrWhiteSpace($entity.Expected.ExtendedProperties.$property)) {
                    $extendedProperty = Add-BT_ExtendedProperty -Ticket $mspcObject.WorkgroupTicket `
                        -ReferenceEntityId $newUser.Id -ReferenceEntityType "CustomerEndUser" -Name $property `
                        -Value $entity.Expected.ExtendedProperties.$property -Environment $environment
                    if (!$extendedProperty) {
                        $extendedPropertyErrors += "Failed to create extended property $($property):$($entity.Expected.ExtendedProperties.$property)."
                        continue
                    }
                }
            }
            if (![String]::IsNullOrWhiteSpace($extendedPropertyErrors)) {
                throw $extendedPropertyErrors
            }
            "success"
        }
        CompareEntities = {
            # Error output is redirected to null to ensure that only the $true/$false result of the compare is returned
            Compare-MSPCompleteUser -ReferenceUser $entity.Expected -ComparisonUser $entity.Current 2>$null
        }
        UpdateEntity = {
            # Create hash table for params
            $setBTCustomerEndUserParams = @{
                Ticket          = $mspcObject.CustomerTicket
                CustomerEndUser = $entity.Current
                Environment     = $environment
            }

            # Add built-in properties to the params
            Get-MSPCompleteUserPropertyList | ForEach-Object -Process {
                if ($entity.Expected.$_) {
                    $setBTCustomerEndUserParams.Add($_, $entity.Expected.$_)
                }
            }

            # Update the user
            $updatedUser = Set-BT_CustomerEndUser @setBTCustomerEndUserParams
            if (!$updatedUser) {
                throw "Failed to update user."
            }

            # Update the user extended properties
            $extendedPropertyErrors = ""
            $extendedProperties = Get-MSPCompleteUserExtendedPropertyList
            foreach ($property in $extendedProperties) {
                # Only update/create if different and the new value is not null or whitespace
                if ($entity.Current.ExtendedProperties.$property -ne $entity.Expected.ExtendedProperties.$property -and ![String]::IsNullOrWhiteSpace($entity.Expected.ExtendedProperties.$property)) {
                    $extendedProperty = Get-BT_ExtendedProperty -Ticket $mspcObject.WorkgroupTicket `
                        -ReferenceEntityId $updatedUser.Id -ReferenceEntityType "CustomerEndUser" `
                        -Name $property -Environment $environment

                    # Update existing extended property
                    if ($extendedProperty) {
                        $extendedProperty = Set-BT_ExtendedProperty -Ticket $mspcObject.WorkgroupTicket `
                            -ExtendedProperty $extendedProperty -Value $entity.Expected.ExtendedProperties.$property -Environment $environment
                        if (!$extendedProperty) {
                            $extendedPropertyErrors += "Failed to update extended property $($property):$($entity.Expected.ExtendedProperties.$property)."
                            continue
                        }
                    }

                    # Create new extended property
                    else {
                        $extendedProperty = Add-BT_ExtendedProperty -Ticket $mspcObject.WorkgroupTicket `
                            -ReferenceEntityId $updatedUser.Id -ReferenceEntityType "CustomerEndUser" `
                            -Name $property -Value $entity.Expected.ExtendedProperties.$property -Environment $environment
                        if (!$extendedProperty) {
                            $extendedPropertyErrors += "Failed to create extended property $($property):$($entity.Expected.ExtendedProperties.$property)."
                            continue
                        }
                    }
                }
            }
            if (![String]::IsNullOrWhiteSpace($extendedPropertyErrors)) {
                throw $extendedPropertyErrors
            }
            "success"
        }
        ArchiveEntity = {
            # Archive the user
            $archivedUser = Set-BT_CustomerEndUser -Ticket $mspcObject.CustomerTicket -CustomerEndUser $entity.Current `
                                -IsArchived $true -Environment $environment
            if (!$archivedUser) {
                throw "Failed to archive user."
            }
            "success"
        }
        DeleteEntity = {
            # Delete the user
            Remove-BT_CustomerEndUser -Ticket $mspcObject.CustomerTicket -Id $entity.Current.Id `
                -Force -Environment $environment
            "success"
        }
    }
}