Private/Sync-Objects.ps1

<#
.NOTES
    Company: BitTitan, Inc.
    Title: Sync-Objects.ps1
    Author: SUPPORT@BITTITAN.COM
    Requirements:
     
    Version: 1.1
    Date: DECEMBER 22, 2016
 
    Disclaimer: This script is provided ‘AS IS’. No warrantee is provided either expresses or implied.
 
    Copyright: Copyright© 2016 BitTitan. All rights reserved.
     
.SYNOPSIS
  Uses a switch function to determine what kind of sync/delete operation to perform against AD objects.
 
.DESCRIPTION
  Helper function for the Sync-ADtoO365 tool.
 
#>


function Sync-Objects 
{
[CmdletBinding()]

Param (
    
    [int]$objectAction,
    [int]$syncAction,
    
    [System.Management.Automation.Credential()]$MigrationWizCredentials,
    [System.Management.Automation.Credential()]$O365Credentials

)

Begin {}

Process 
{
    
    switch($syncAction)
    {
        0 # Simulate without delete
        {
            $simulate = $true
            $delete = $false
        }
        1 # Simulate with delete
        {
            $simulate = $true
            $delete = $true
        }
        2 # Sync without delete
        {
            $simulate = $false
            $delete = $false
        }
        3 # Sync with delete
        {
            $simulate = $false
            $delete = $true
        }
    }
    
    switch($objectAction)
    {
        0 # All objects
        {
            Try 
            {
                Write-Debug "Attempting to sync All (users, groups and contacts)"
                $ErrorActionPreference = 'Stop'
                $sync = SyncAll -ticket ( GetTicket -MigrationWizCredentials $MigrationWizCredentials -environment $environment -Verbose -ErrorAction Stop ) -simulate $simulate -delete $delete
            }
            Catch 
            {
                throw
            }
            Finally 
            {
                $ErrorActionPreference = 'Continue'
            }
        }
        
        1 # Users only
        {
            Try 
            {
                Write-Debug "Attempting to sync User objects"
                $ErrorActionPreference = 'Stop'
                $sync = SyncUsers -ticket ( GetTicket -MigrationWizCredentials $MigrationWizCredentials -environment $environment -Verbose -ErrorAction Stop ) -simulate $simulate -delete $delete
            }
            Catch 
            {
                throw
            }
            Finally 
            {
                $ErrorActionPreference = 'Finally'
            }
        }
        
        2 # Contacts only
        {
            Try 
            {
                Write-Debug "Attempting to sync Contacts"
                $ErrorActionPreference = 'Stop'
                $sync = SyncContacts -ticket ( GetTicket -MigrationWizCredentials $MigrationWizCredentials -environment $environment -Verbose -ErrorAction Stop ) -simulate $simulate -delete $delete
            }
            Catch 
            {
                throw
            }
            Finally 
            {
                $ErrorActionPreference = 'Continue'    
            }
        }
        
        3 # Groups only
        {
            Try 
            {
                Write-Debug "Attempting to sync Groups"
                $ErrorActionPreference = 'Stop'
                $sync = SyncGroups -ticket ( GetTicket -MigrationWizCredentials $MigrationWizCredentials -environment $environment -Verbose -ErrorAction Stop ) -simulate $simulate -delete $delete
            }
            Catch 
            {
                throw
            }
            Finally 
            {
                $ErrorActionPreference = 'Continue'
            }
        }
    }

    return $sync

} # end process block

End {}

} # end Sync-Objections function