Private/Get-SyncAction.ps1

<#
.NOTES
    Company: BitTitan, Inc.
    Title: Get-SyncAction.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
  Prompts the user for input (and validates it), and returns an integer that corresponds with a sync/delete operation.
 
.DESCRIPTION
  Helper function for the Sync-ADtoO365 tool.
 
#>


function Get-SyncAction 
{
[cmdletbinding()]
    
    param (
        [parameter(
            ParameterSetName = 'Interactive'
        )]
        [switch]$Interactive,
        
        [parameter(
            ParameterSetName = 'NonInteractive'
        )]
        [ValidateNotNullorEmpty()]
        [ValidateSet(0, 1, 2, 3)]
        [int]$Option
    )
    Begin {}
    
    Process 
    {
        # Define option set to output running context when running in silent mode (interactive switch omitted)
        $optionSet = @{
            0 = 'Simulate without delete'
            1 = 'Simulate delete'
            2 = 'Sync without delete'
            3 = 'Sync with delete'
        }
    
        # If in silent mode, find the key that correlates with the option specified in the hash-table above, and print it's output
        if ( -not ($Interactive) ) 
        {
            $runningContext = $option
            $Message = $optionSet.Item($runningContext)
            Write-Verbose "Option specified was: $Message"
        }
    
        # user input or specified parameter value will be stored to '$result'
        $result = $null
        # If interactive switch is specified, be prompted with the below
    
        if ( ($Interactive) ) 
        {
            Write-Debug "'Interactive' swtich specified, will prompt for user input `n"
            # Display all sync options to user
            Write-Host "Select synchronization option:" -ForegroundColor Yellow
            Write-Host "0 - Simulate without delete" -ForegroundColor Green 
            Write-Host "1 - Simulate deltete" -ForegroundColor Green 
            Write-Host "2 - Sync without delete" -ForegroundColor Green
            Write-Host "3 - Sync with delete" -ForegroundColor Green
            Write-Host "Ctrl+C - Exit `n"
            
            # enter loop until valid input is received
            while ($true)
            {
                $result = Read-Host -Prompt "Select 0 - 3"
                

                if ( ($result -match "^\d+$") -and ([int]$result -ge 0) -and ([int]$result -le 3) )
                {
                    Write-Host "Option $result specified." -ForegroundColor Green
                    return [int]$result
                }
                elseif ($result -notcontains 0..3)
                {
                    Write-Warning "Invalid input specified: Specify option 0 - 3, or hit 'Ctrl'+'C' to exit"    
                }
            }
            
            if ($null -eq $result)
            {
                Write-Warning 'No valid input specified, returning to console...'
                return $null
            }    
        } # end if block for interactive mode
        
        
        # Else block below will return the value specified in the option parameter
        else 
        {
            Write-Verbose 'Running in NonInteractive mode'
            $result = $Option
            return $result
        }
    
    } # end Process block
    
    End {} # end End block

} #end Get-SyncAction function