Public/VoiceConfig/Get-TeamsVoiceNormalizationRule.ps1

# Module: TeamsFunctions
# Function: VoiceConfig
# Author: David Eberhardt
# Updated: 01-JUL-2022
# Status: Live


#TODO Rework Output Object to display the Dial Plan Name as well? Benefit? Readability, Drawback: ExecutionTime and no longer an MS object...

function Get-TeamsVoiceNormalizationRule {
  <#
  .SYNOPSIS
    Lists Normalization Rules matching a string (Name), part of a Tenant Dial Plan (Parent) or a specific Identity
  .DESCRIPTION
    Get-CsVoiceNormalizationRule is deprecated (removed) in MicrosoftTeams v4. This Cmdlet tries to alleviate this.
    Accepts the Identity (in the format "Dial Plan\NormalizationRule"), the Tenant Dial Plan Name (its Parent),
    or the Normalization Rule Name itself (Name). Wildcards can be used to search with each method.
  .PARAMETER Identity
    String. Name or part of the Normalization Rule in the format "<Teams Dial Plan>\<Normalization Rule Name>".
    Required for ParameterSet Identity. Allows for Wildcard searches.
  .PARAMETER Parent
    String. Name or part of the Teams Dial Plan. Allows for Wildcard searches.
    Required for ParameterSet Parent.
  .PARAMETER Name
    String. Name or part of the Normalization Rule. Allows for Wildcard searches.
    Required for ParameterSet Name. Optional for ParameterSet Parent
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule
 
    Prompts for Identity (mandatory input)
    Attention: Changed Behaviour. Previously, this showed a list of all Tenant Dial Plans, please run Get-TeamsTDP for that.
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Identity DP-HUN
 
    Returns Voice Normalisation Rules from the Tenant Dial Plan DP-HUN (provided it exists).
    Behaviour like: (Get-CsTenantDialPlan -Identity "DP-HUN").NormalizationRules
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Identity DP-HUN*
 
    Returns Voice Normalisation Rules from all Tenant Dial Plans that match "DP-HUN*".
    Behaviour like: (Get-CsTenantDialPlan -Filter "DP-HUN*").NormalizationRules
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Identity DP-HUN*\International
 
    Returns Voice Normalisation Rules "International" from all Tenant Dial Plans that match "DP-HUN*".
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Identity DP-HUN*\*International
 
    Returns Voice Normalisation Rules that match "*International" from all Tenant Dial Plans that match "DP-HUN*".
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Parent DP-HUN
 
    Returns Voice Normalisation Rules from the Tenant Dial Plan DP-HUN (provided it exists).
    Behaviour like: (Get-CsTenantDialPlan -Identity "DP-HUN").NormalizationRules
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Parent DP-HUN*
 
    Returns Voice Normalisation Rules from all Tenant Dial Plans that match "DP-HUN*".
    Behaviour like: (Get-CsTenantDialPlan -Filter "DP-HUN*").NormalizationRules
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Parent DP-SLO -Name International
 
    Returns Voice Normalisation Rules "International" from the Tenant Dial Plans "DP-SLO".
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Parent DP-SLO* -Name *Short*
 
    Returns Voice Normalisation Rules that match "*Short*" from all Tenant Dial Plans that match "DP-SLO*".
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Name "EmergencyService"
 
    Returns Voice Normalisation Rules "EmergencyService" from all Tenant Dial Plans except Global.
  .EXAMPLE
    Get-TeamsVoiceNormalizationRule -Name *Short*
 
    Returns Voice Normalisation Rules that match "*Short*" from all Tenant Dial Plans except Global.
  .INPUTS
    System.String
  .OUTPUTS
    System.Object
  .NOTES
    None
  .COMPONENT
    SupportingFunction
    VoiceConfiguration
  .FUNCTIONALITY
    Queries Normalization Rules from Tenant Dial Plan(s) from the Tenant
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/Get-TeamsVoiceNormalizationRule.md
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/about_VoiceConfiguration.md
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/about_Supporting_Functions.md
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/
  #>


  [CmdletBinding(DefaultParameterSetName = 'Identity')]
  [Alias('Get-TeamsVNR')]
  param (
    [Parameter(Position = 0, Mandatory, ParameterSetName = 'Identity', HelpMessage = 'Name of the DialPlan\Normalization Rule')]
    [ArgumentCompleter({ &$global:TfAcSbTenantDialPlanNormalizationRuleIds })]
    [string[]]$Identity,

    [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'Parent', HelpMessage = 'Name of the Dial Plan')]
    [Alias('SimpleName')]
    [ArgumentCompleter({ &$global:TfAcSbTenantDialPlan })]
    [string]$Parent,

    [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'Name', HelpMessage = 'Name of the Normalization Rule')]
    [Parameter(ParameterSetName = 'Parent', HelpMessage = 'Name of the Normalization Rule')]
    [string]$Name
  )
  begin {
    Show-FunctionStatus -Level Live
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"
    Write-Verbose -Mess"Need help? Online: $global:TeamsFunctionsHelpURLBase$($MyInvocation.MyCommand)`.md"

    # Asserting MicrosoftTeams Connection
    if ( -not (Assert-MicrosoftTeamsConnection) ) { break }

    function GetVNR {
      [CmdletBinding()]
      param (
        [string]$Parent,
        [string]$Name
      )

      # Querying Dial Plans
      Write-Verbose -Message "Dial Plan '$Parent' - Finding Tenant Dial Plan that match '$Parent'"
      try {
        $Filtered = $null
        if ($Parent -match [regex]::Escape('*')) {
          $Filtered = Get-CsTenantDialPlan -WarningAction SilentlyContinue -Filter "*$Parent*" -ErrorAction Stop
        }
        else {
          $Filtered = Get-CsTenantDialPlan -WarningAction SilentlyContinue -Identity "Tag:$Parent" -ErrorAction Stop
        }
      }
      catch {
        Write-Information "INFO: No Tenant Dial Plan '$Parent' found in the Tenant" -InformationAction Continue
      }

      # Filtering for Normalization Rule ($Name) - if present
      if ( $PsBOundParameters.ContainsKey('Name')) {
        if ( $Name -match [regex]::Escape('*') ) {
          Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule Names that match '$Name'"
          $NormalizationRules = $Filtered.NormalizationRules | Where-Object Name -Like "*$Name*"
        }
        elseif ( $Name -eq '' ) {
          Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule Names (any)"
          $NormalizationRules = $Filtered.NormalizationRules
        }
        else {
          Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule Names that are equal to '$Name'"
          $NormalizationRules = $Filtered.NormalizationRules | Where-Object Name -EQ "$Name"
        }
        return $NormalizationRules | Select-Object Name, Pattern, Translation, Description
      }
      else {
        return $Filtered.NormalizationRules | Select-Object Name, Pattern, Translation, Description
      }
    }
  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"

    switch ( $PSCmdlet.ParameterSetName ) {
      'Identity' {
        Write-Verbose -Message "Normalization Rule Identity '$Identity' - Finding Tenant Dial Plans"
        $Parent = $Name = $null
        if ( $Identity -match [regex]::Escape('\') -or $Identity -match [regex]::Escape('/') ) {
          $Parent, $Name = $Identity.Replace('/', '\').Split('\')
        }
        else {
          $Parent = $Identity
        }
        # Calling itself with $Parent and $Name respectively
        $GetTeamsVoiceNormalizationRule = $null
        $GetTeamsVoiceNormalizationRule += @{ 'Parent' = "$Parent" }
        if ( $null -ne $Name ) { $GetTeamsVoiceNormalizationRule += @{ 'Name' = "$Name" } }
        [void]$PSBoundParameters.Remove('Identity')
        GetVNR @GetTeamsVoiceNormalizationRule @PSBoundParameters
      }
      'Parent' {
        # Determining list of Dial Plans ($Parent)
        Write-Verbose -Message "Dial Plan '$Parent' - Finding Tenant Dial Plans"
        GetVNR @PSBoundParameters
      }
      'Name' {
        Write-Verbose -Message "Rule '$Name' - Finding Tenant Dial Plan Names"
        $Filtered = Get-CsTenantDialPlan | Where-Object Identity -NE 'Global' | Sort-Object Identity
        # Calling itself with $Parent and $Name respectively
        $Filtered | ForEach-Object {
          $GetTeamsVoiceNormalizationRule = $null
          $GetTeamsVoiceNormalizationRule += @{ 'Parent' = $_.SimpleName }
          #$GetTeamsVoiceNormalizationRule += @{ 'Name' = "$Name" }
          #Get-TeamsVoiceNormalizationRule @GetTeamsVoiceNormalizationRule
          GetVNR @GetTeamsVoiceNormalizationRule @PSBoundParameters
        }
      }
    }
  } #process

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