Private/Format-VoiceRoutingChainInput.ps1

# Module: TeamsFunctions
# Function: Teams Voice Routing Chain
# Author: David Eberhardt
# Updated: 26-AUG-2022
# Status: BETA




function Format-VoiceRoutingChainInput {
  <#
    .SYNOPSIS
        Helper function to deconstruct the Voice Routing Chain Input string
    .DESCRIPTION
    Helper function to deconstruct the Voice Routing Chain Input string
    Used in New-TeamsVoiceRoutingChain
  .PARAMETER String
    The input string to transform
  .PARAMETER Name
    String. Proto-Name for the Routing Chain: Policy, PstnUsage and Voice Route.
    Prefixes, Interfixes and Suffixes are attached afterwards depending on Artifact, Country, Site & Call Restriction
  .PARAMETER Region
    GeoRegion: AMER, EMEA or APAC
  .PARAMETER Country
    String. ISO 3166-alpha2 or -alpha3 2/3-digit Country Code
  .PARAMETER Site
    Optional String (3-10 digits). Site Identifier
  .PARAMETER CallRestriction
    Optional String. Unrestricted, International, National, EmergencyOnly or Custom
  .PARAMETER CustomRestrictionName
    Required String (3-10 digits) for Custom CallRestrictions only.
  .INPUTS
    System.String
    Boolean
  .OUTPUTS
    PSCustomObject
  .NOTES
    Step 1 - Format-VoiceRoutingChainInput - Deconstructs the string based on input.
    Step 2 - Format-VoiceRoutingChainOutput - Construct new name based on requirement
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/
  .LINK
    New-TeamsVoiceRoutingChain
    #>


  [CmdletBinding()]
  [OutputType([PSCustomObject])]
  param(
    [Parameter()]
    [ValidatePattern('^[a-zA-Z0-9-_]+$')]
    [String]$Name,

    [Parameter(Mandatory)]
    [GeoRegion]$Region,

    [Parameter(HelpMessage = 'ISO 3166-alpha2 Country Code')]
    [string]$Country,

    [Parameter(HelpMessage = 'Site-Specific name')]
    [String]$Site,

    [Parameter(HelpMessage = 'Call Restriction Level')]
    [String]$CallRestriction,

    [Parameter(HelpMessage = 'Custom Name for the Call Restriction Level')]
    [String]$CustomRestrictionName
  ) #param

  begin {
    #Show-FunctionStatus -Level Live
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    # Setting Preference Variables according to Upstream settings
    if (-not $PSBoundParameters.ContainsKey('Verbose')) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') }
    if (-not $PSBoundParameters.ContainsKey('Confirm')) { $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference') }
    if (-not $PSBoundParameters.ContainsKey('WhatIf')) { $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference') }
    if (-not $PSBoundParameters.ContainsKey('Debug')) { $DebugPreference = $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } else { $DebugPreference = 'Continue' }
    if ( $PSBoundParameters.ContainsKey('InformationAction')) { $InformationPreference = $PSCmdlet.SessionState.PSVariable.GetValue('InformationAction') } else { $InformationPreference = 'Continue' }

    [string]$ProtoName = $Name
    # Initialising Empty Hashtable
    $Output = [PSCustomObject][ordered]@{
      Name            = ''
      Site            = $false
      Country         = $false
      Region          = $false
      CallRestriction = $false
      Prefix          = $false
    }

  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand) - Processing Voice Routing Chain Name '$ProtoName'"


    if ( $PSBoundParameters.ContainsKey('Site') ) {
      Write-Verbose -Message "Site '$Site' - Checking for duplication"
      if ( $ProtoName -match "(-|_)(?<Site>($Site))" ) {
        $ProtoName = $ProtoName -replace "^(.*?)((-|_)($($Matches.Site)))(.*)?$", '$1$5'
        $Output.Site = $true
        Write-Verbose -Message "Site '$($Matches.Site)' discovered in provided Name. String deconstructed to '$ProtoName'"
      }
      else {
        Write-Verbose -Message "Site '$Site' not discovered in provided Name. String retained as '$ProtoName'"
      }
    }
    else {
      Write-Verbose -Message 'Site not provided'
    }

    if ( $PSBoundParameters.ContainsKey('Country') ) {
      Write-Verbose -Message "Country '$Country' - Checking for duplication"
      if ( $ProtoName -match "(-|_)(?<CountryCode>($Country))(-|_)?" ) {
        $ProtoName = $ProtoName -replace "^(.*?)((-|_)($($Matches.CountryCode)))(.*)?$", '$1$5'
        $Output.Country = $true
        Write-Verbose -Message "Country '$($Matches.CountryCode)' discovered in provided Name. String deconstructed to '$ProtoName'"
      }
      else {
        Write-Verbose -Message "Country '$Country' not discovered in provided Name. String retained as '$ProtoName'"
      }
    }
    else {
      Write-Verbose -Message 'Country not provided'
    }

    # Region is mandatory
    Write-Verbose -Message "Region '$Region' - Checking for duplication"
    if ( $ProtoName -match "(-|_)(?<Region>(EMEA|AMER|APAC|$Region))" ) {
      $ProtoName = $ProtoName -replace "^(.*?)((-|_)($($Matches.Region)))(.*)?$", '$1$5'
      $Output.Region = $true
      Write-Verbose -Message "Region '$($Matches.Region)' discovered in provided Name. String deconstructed to '$ProtoName'"
    }
    else {
      Write-Verbose -Message "Region '$Region' not discovered in provided Name. String retained as '$ProtoName'"
    }

    if ( $PSBoundParameters.ContainsKey('CallRestriction') ) {
      Write-Verbose -Message "Call Restriction '$CallRestriction' - Checking for duplication"
      $RestrictionMatchString = "((-|_)(?<Restriction>(Unrestricted|International|INT|National|NAT|EmergencyOnly|EMS|Restricted|$(if( $CustomRestrictionName ) { $CustomRestrictionName } else { 'Custom' }))))"
      if ( $ProtoName -match $RestrictionMatchString ) {
        $ProtoName = $ProtoName -replace "^(.*?)(-|_)?($($matches.Restriction))$", '$1'
        $Output.CallRestriction = $true
        Write-Verbose -Message "CallRestriction '$($matches.Restriction)' discovered in provided Name. String deconstructed to '$ProtoName'"
      }
      else {
        Write-Verbose -Message "CallRestriction '$CallRestriction' not discovered in the Name. String retained as '$ProtoName'"
      }
    }
    else {
      Write-Verbose -Message 'CallRestriction not provided'
    }

    Write-Verbose -Message "Prefix 'OVP' - Checking for duplication"
    if ( $ProtoName -match 'OVP' -or $ProtoName -match '^(?<OVPprefix>OVP)(-|_)') {
      if ( $ProtoName -eq 'OVP' ) {
        $ProtoName = ''
        $Output.Prefix = $true
        Write-Verbose -Message "Prefix 'OVP' discovered as the remainder of the Name. String deconstructed to '$ProtoName'"
      }
      elseif ( $ProtoName -match '^(?<OVPprefix>OVP)(-|_)' ) {
        $ProtoName = $ProtoName -replace "^($($Matches.OVPprefix))(-|_)?(.*)?$", '$3'
        $Output.Prefix = $true
        Write-Verbose -Message "Prefix '$($Matches.OVPprefix)' discovered in provided Name. String deconstructed to '$ProtoName'"
      }
      else {
        Write-Verbose -Message "Prefix 'OVP' matched but not discovered properly. String retained as '$ProtoName'"
      }
    }
    else {
      Write-Verbose -Message "Prefix 'OVP' not discovered in the Name. String retained as '$ProtoName'"
    }

    # Remaining ProtoName used as Name
    Write-Verbose -Message "[FINISH] $($MyInvocation.MyCommand) - Processing Voice Routing Chain Name '$ProtoName'"
    $Output.Name = $ProtoName

    # Output
    return $Output

  } #process

  end {
    Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)"
  } #end

} # Format-VoiceRoutingChainInput