Private/Get-ObjectAction.ps1

<#
.NOTES
    Company: BitTitan, Inc.
    Title: Get-ObjectAction.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 the type(s) of objects to synchronize.
 
.DESCRIPTION
  Helper function for the Sync-ADtoO365 tool.
 
#>


function Get-ObjectAction 
{
[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 = 'Users, Contacts and Groups'
            1 = 'Users only'
            2 = 'Contacts only'
            3 = 'Groups only'
        }
        
        # 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
            $verboseMessage = $optionSet.Item($runningContext)
            Write-Verbose "Option specified was: $verboseMessage"
        }
        
        # 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"
            Write-Host "Select the object types to synchronize:" -ForegroundColor Yellow
            Write-Host "0 - Users, Contacts and Groups" -ForegroundColor Green
            Write-Host "1 - Users only" -ForegroundColor Green
            Write-Host "2 - Contacts only" -ForegroundColor Green
            Write-Host "3 - Groups only" -ForegroundColor Green
            Write-Host "Ctrl+C - Exit `n" -ForegroundColor Green
            
            # enter loop until user provides valid input
            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 select Ctrl+C to exit"    
                }
            }
            
            if ($result -eq $null)
            {
                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-ObjectAction function