Public/VoiceConfig/Remove-TeamsVoiceNormalizationRule.ps1

# Module: TeamsFunctions
# Function: VoiceConfig
# Author: David Eberhardt
# Updated: 01-JAN-2021
# Status: Live




function Remove-TeamsVoiceNormalizationRule {
  <#
  .SYNOPSIS
    Removes Normalization Rules by Name and its Tenant Dial Plan (Parent) or a specific Identity
  .DESCRIPTION
    Remove-CsVoiceNormalizationRule is deprecated (removed) in MicrosoftTeams v4. This Cmdlet tries to alleviate this.
    Accepts the Identity (in the format "Dial Plan\NormalizationRule") or the Tenant Dial Plan Name (its Parent),
    and the Normalization Rule Name itself (Name). Wildcards are disallowed to perform clean removals.
  .PARAMETER Identity
    String. Name or part of the Normalization Rule in the format "<Teams Dial Plan>\<Normalization Rule Name>".
    Required for ParameterSet Identity.
  .PARAMETER Parent
    String. Name or part of the Teams Dial Plan.
    Required for ParameterSet Parent.
  .PARAMETER Name
    String. Name or part of the Normalization Rule.
    Required for ParameterSet Parent
  .EXAMPLE
    Remove-TeamsVoiceNormalizationRule -Identity DP-HUN\HUN-International
 
    Removes Voice Normalisation Rule "HUN-International" from the Tenant Dial Plan DP-HUN.
    String. Identity of the Normalization Rule in the format "<Teams Dial Plan>\<Normalization Rule Name>".
  .EXAMPLE
    Remove-TeamsVoiceNormalizationRule -Identity DP-HUN -Name HUN-International
 
    Removes Voice Normalisation Rule "HUN-International" from the Tenant Dial Plan DP-HUN.
  .INPUTS
    System.String
  .OUTPUTS
    System.Object
  .NOTES
    None
  .COMPONENT
    SupportingFunction
    VoiceConfiguration
  .FUNCTIONALITY
    Removes a Normalization Rule from a Tenant Dial Plan from the Tenant
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/Remove-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', SupportsShouldProcess, ConfirmImpact = 'Medium')]
  [Alias('Remove-TeamsVNR')]
  param (
    [Parameter(Position = 0, Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'Identity', HelpMessage = 'Name of the DialPlan\Normalization Rule')]
    [ValidateScript( {
        if ($_ -match [regex]::Escape('\') -or $Identity -match [regex]::Escape('/')) { $true } else {
          throw [System.Management.Automation.ValidationMetadataException] "Identity must include the DialPlanName and the NormalizationRuleName separated by '/'"
        } })]
    [string[]]$Identity,

    [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'Parent', HelpMessage = 'Name of the Dial Plan')]
    [ValidateScript( {
        if ($_ -in $(&$global:TfAcSbTenantDialPlan)) { return $true } else {
          throw [System.Management.Automation.ValidationMetadataException] 'Value must be a valid Dial Plan in the Tenant. Use Intellisense for options'
        } })]
    [ArgumentCompleter({ &$global:TfAcSbVoiceRoutingPolicy })]
    [string]$Parent,

    [Parameter(Mandatory, ValueFromPipelineByPropertyName, 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 RemoveVNR {
      [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
      param (
        [string]$Parent,
        [string]$Name
      )

      # Querying Dial Plans
      Write-Verbose -Message "Dial Plan '$Parent' - Finding Tenant Dial Plan '$Parent'"
      try {
        $DialPlan = $null
        $DialPlan = Get-CsTenantDialPlan -Identity "Tag:$Parent" -WarningAction SilentlyContinue -ErrorAction Stop
      }
      catch {
        Write-Information "INFO: No Tenant Dial Plan '$Parent' found in the Tenant" -InformationAction Continue
      }

      # Finding Normalization Rule
      Write-Verbose -Message "Dial Plan '$Parent' - Finding Normalization Rule '$Name'"
      $Rule = $null
      $Rule = $DialPlan.NormalizationRules | Where-Object Name -EQ "$Name"
      if ( $null -ne $Rule ) {
        if ($PSCmdlet.ShouldProcess("$Id", "Remove NormalizationRule from Tenant Dial Plan '$DialPlan'")) {
          [void]$DialPlan.NormalizationRules.Remove($Rule)
          Set-CsTenantDialPlan -Identity $Parent -NormalizationRules $DialPlan.NormalizationRules
        }
      }
      else {
        Write-Information "INFO: No Normalization Rule '$Name' found in Tenant Dial Plan '$Parent'" -InformationAction Continue
      }
    }
  } #begin

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

    switch ( $PSCmdlet.ParameterSetName ) {
      'Identity' {
        foreach ( $Id in $Identity ) {
          Write-Verbose -Message "Normalization Rule Identity '$Id' - Finding Tenant Dial Plans"
          $Parent = $Name = $null
          $Parent, $Name = $Id.Replace('/', '\').Split('\')
          [void]$PSBoundParameters.Remove('Identity')
          RemoveVNR -Parent $Parent -Name $Name @PSBoundParameters
        }
      }
      'Parent' {
        # Determining list of Dial Plans ($Parent)
        Write-Verbose -Message "Dial Plan '$Parent' - Finding Tenant Dial Plans"
        RemoveVNR @PSBoundParameters
      }
    }
  } #process

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