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

function Get-SyncMSPCompleteCustomersScriptBlocks {
    # Return the script blocks
    return @{
        UnArchiveEntity = {
            # Retrieve the archived customer (if it exists)
            $archivedCustomer = Get-BT_Customer -Ticket $mspcObject.Ticket -CompanyName $entity.Expected.CompanyName `
                -WorkgroupId $mspcObject.Workgroup.Id -IsArchived $true -IsDeleted $false -Environment $environment

            # The archived customer doesn't exist
            if (!$archivedCustomer) {
                return
            }

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

            # Un-archive the customer
            $unArchivedCustomer = Set-BT_Customer -Ticket $mspcObject.Ticket -Customer $archivedCustomer `
                -IsArchived $false -Environment $environment
            if (!$unArchivedCustomer) {
                throw "Failed to un-archive previously archived customer."
            }
            "success"
        }
        CreateEntity = {
            # Create hash table for params
            $addBTCustomerParams = @{
                Ticket      = $mspcObject.Ticket
                Environment = $environment
            }

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

            # Create the customer
            $newCustomer = Add-BT_Customer @addBTCustomerParams
            if (!$newCustomer) {
                throw "Failed to create customer."
            }
            "success"
        }
        CompareEntities = {
            Compare-MSPCompleteCustomer -ReferenceCustomer $entity.Expected -ComparisonCustomer $entity.Current 2>$null
        }
        UpdateEntity = {
            # Create hash table for params
            $setBTCustomerParams = @{
                Ticket      = $mspcObject.Ticket
                Customer    = $entity.Current
                Environment = $environment
            }

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

            # Update the customer
            $updatedCustomer = Set-BT_Customer @setBTCustomerParams
            if (!$updatedCustomer) {
                throw "Failed to update customer."
            }
            "success"
        }
        ArchiveEntity = {
            # Archive the customer
            $archivedCustomer = Set-BT_Customer -Ticket $mspcObject.Ticket -Customer $entity.Current `
                -WorkgroupId $mspcObject.Workgroup.Id -IsArchived $true -Environment $environment
            if (!$archivedCustomer) {
                throw "Failed to archive customer."
            }
            "success"
        }
        DeleteEntity = {
            # Delete the customer
            Remove-BT_Customer -Ticket $mspcObject.Ticket -Id $entity.Current.Id `
                -Force -Environment $environment
            "success"
        }
    }
}