Public/VoiceConfig/VoiceNormalizationRule/Get-TeamsVoiceNormalizationRule.ps1

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




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/Orbit/tree/main/docs/Orbit.Teams/Get-TeamsVoiceNormalizationRule.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_VoiceConfiguration.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  #>


  [CmdletBinding(DefaultParameterSetName = 'Identity')]
  [Alias('Get-TeamsVNR')]
  param (
    [Parameter(Position = 0, Mandatory, ParameterSetName = 'Identity', HelpMessage = 'Name of the DialPlan\Normalization Rule')]
    [ValidateScript( {
        if ($_ -in $( Get-OrbitAcSbTenantDialPlanNormalizationRuleIds @args )) { return $true } else {
          throw [System.Management.Automation.ValidationMetadataException] 'Value must be a valid Dial Plan Normalization Rule in the Tenant. Use Intellisense for options'
        } })]
    [string[]]$Identity,

    [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'Parent', HelpMessage = 'Name of the Dial Plan')]
    [Alias('SimpleName')]
    [ValidateScript( {
        if ($_ -in $( Get-OrbitAcSbTenantDialPlan @args )) { return $true } else {
          throw [System.Management.Automation.ValidationMetadataException] 'Value must be a valid Dial Plan in the Tenant. Use Intellisense for options'
        } })]
    [string]$Parent,

    [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'Name', HelpMessage = 'Name of the Normalization Rule')]
    [Parameter(ParameterSetName = 'Parent', HelpMessage = 'Name of the Normalization Rule')]
    [string]$Name,

    [Parameter(HelpMessage = 'Include the Dial Plan Name in the output')]
    #[Parameter(ParameterSetName = 'Parent', HelpMessage = 'Include the Dial Plan Name in the output')]
    [switch]$IncludeTDP
  )
  begin {
    Show-OrbitFunctionStatus -Level Live
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"
    Write-Verbose -Message "Need help? Online: $global:OrbitHelpURLBase$($MyInvocation.MyCommand)`.md"

    # using class OrbitVoiceNormalizationRule, defined in Module scope

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

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

      # 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
      }

      if ( $IncludeTDP.IsPresent ) {
        # This may not be fast, but adds the Dial Plan Name before outputting the Rules
        foreach ( $TDP in $Filtered ) {
          [System.Collections.Generic.List[object]]$NormalizationRules = @()
          # Filtering for Normalization Rule ($Name) - if present
          if ( $PSBoundParameters['Name']) {
            if ( $Name -match [regex]::Escape('*') ) {
              Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule Names that match '$Name'"
              $Rules = $TDP.NormalizationRules | Where-Object Name -Like "*$Name*"
              $Rules | ForEach-Object {
                [void]$NormalizationRules.Add([OrbitVoiceNormalizationRule]::new($TDP.SimpleName, $_.Name, $_.Description, $_.Pattern, $_.Translation, $_.IsInternalExtension))
              }
            }
            elseif ( $Name -eq '' ) {
              Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule Names (any)"
              $Filtered.NormalizationRules | ForEach-Object {
                [void]$NormalizationRules.Add([OrbitVoiceNormalizationRule]::new($TDP.SimpleName, $_.Name, $_.Description, $_.Pattern, $_.Translation, $_.IsInternalExtension))
              }
            }
            else {
              Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule Names that are equal to '$Name'"
              $Rules = $Filtered.NormalizationRules | Where-Object Name -EQ "$Name"
              $Rules | ForEach-Object {
                [void]$NormalizationRules.Add([OrbitVoiceNormalizationRule]::new($TDP.SimpleName, $_.Name, $_.Description, $_.Pattern, $_.Translation, $_.IsInternalExtension))
              }
            }
          }
          else {
            $Filtered.NormalizationRules | ForEach-Object {
              [void]$NormalizationRules.Add([OrbitVoiceNormalizationRule]::new($TDP.SimpleName, $_.Name, $_.Description, $_.Pattern, $_.Translation, $_.IsInternalExtension))
            }
          }
          Write-Output $NormalizationRules
        }
      }
      else {
        # This is fast, but does not contain the DialPlanName
        # Filtering for Normalization Rule ($Name) - if present
        if ( $PSBoundParameters['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 = @{}
        $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
          GetVNR @GetTeamsVoiceNormalizationRule @PSBoundParameters
        }
      }
    }
  } #process

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