Halo PSA: Deduplicate Contacts

I was recently testing out a new version of the Halo PSA PowerShell module and for completely unrelated reasons I needed to then write a contact deduplication script ;). This will run the built in re-assign function for tickets from any duplicated contacts within a client into the duplicated contact which has the lowest ID number (The one that would have been created first). It will then set the other duplicated contacts to inactive. It does a match on contacts with the same email address inside a single customer. Remember children don’t test in prod!

#### Halo Settings ####
$VaultName = "Your Key Vault"
$HaloClientID = Get-AzKeyVaultSecret -VaultName $VaultName -Name "HaloClientID" -AsPlainText
$HaloClientSecret = Get-AzKeyVaultSecret -VaultName $VaultName -Name "HaloClientSecret" -AsPlainText
$HaloURL = Get-AzKeyVaultSecret -VaultName $VaultName -Name "HaloURL" -AsPlainText


# Connect to Halo
Connect-HaloAPI -URL $HaloURL -ClientId $HaloClientID -ClientSecret $HaloClientSecret -Scopes "all"

$HaloContacts = Get-HaloUser -FullObjects

$DuplicateContacts = $HaloContacts | where-object { $_.emailaddress -ne '' -and $_.emailaddress } | group-object emailaddress, client_id | Where-Object { $_.count -gt 1 }

foreach ($contacts in $DuplicateContacts) {
    $MergeContacts = $Contacts.group | Sort-Object id
    $MergeLength = $MergeContacts.count - 1
    Write-Host "Processing $($MergeContacts[0].emailaddress)"
    foreach ($MergeItem in $MergeContacts[1..$MergeLength]) {
        Write-Host "Merging $($MergeItem.id) with $($MergeContacts[0].id)"
        $UpdateUser = @{
            id                    = $MergeItem.id
            _reassign_all_to_user = $MergeContacts[0].id
        }
        $Null = Set-HaloUser -User $UpdateUser
        $InactiveUser = @{
            id       = $MergeItem.id
            inactive = $true
        }
        $Null = Set-HaloUser -User $InactiveUser
    }
}


You may also like...