Private/Get-TeamAndChannel.ps1

# Module: TeamsFunctions
# Function: Assertion
# Author: David Eberhardt
# Updated: 15-MAY-2021
# Status: Live




function Get-TeamAndChannel {
  <#
    .SYNOPSIS
        Queries Team and Channel based on input
    .DESCRIPTION
    Used by Get-TeamsCallableEntity
  .PARAMETER String
    String in on of the formats:
    TeamId\ChannelId, TeamId\ChannelDisplayName, TeamDisplayName,ChannelId or TeamDisplayName\ChannelDisplayName
    .EXAMPLE
    Get-TeamAndChannel -String "00000000-0000-0000-0000-000000000000\19:abcdef1234567890abcdef1234567890@thread.tacv2"
 
    String is expected in the format "<Team Guid or DN>\<Channel Guid or DN>"
  .NOTES
    This helper function is targeted by Get-TeamsCallQueue as well as Get-TeamsCallableEntity
    Avoids having to wait for Get-TeamsCallableEntity
  #>


  [CmdletBinding()]
  [OutputType([PSCustomObject[]])]
  param(
    [string]$String
  ) #param

  #Show-FunctionStatus -Level Live

  # Setting Preference Variables according to Upstream settings
  if (-not $PSBoundParameters.ContainsKey('Verbose')) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') }
  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' }

  Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand) - Processing Team & Channel for: '$String'"
  $TeamId, $ChannelId = $String.split('\')

  if ($PSBoundParameters.ContainsKey('Debug') -or $DebugPreference -eq 'Continue') {
    " Function: $($MyInvocation.MyCommand.Name) - Team:", ($TeamId | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
    " Function: $($MyInvocation.MyCommand.Name) - Channel:", ($ChannelId | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
  }

  # Finding and returning Team
  try {
    $Team = $null
    if ($TeamId -match $script:TFMatchGuid) {
      Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Team with GroupId: '$TeamId'"
      $Team = Get-Team -GroupId $TeamId -ErrorAction Stop
    }
    else {
      Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Team with DisplayName: '$TeamId'"
      $Team = Get-Team -DisplayName "$TeamId" -ErrorAction Stop
    }

    if ( $Team ) {
      if ($PSBoundParameters.ContainsKey('Debug') -or $DebugPreference -eq 'Continue') {
        " Function: $($MyInvocation.MyCommand.Name) - Team:", ($Team | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
      }

      # dealing with potential duplicates
      if ( $Team.Count -gt 1 ) {
        Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but multiple Teams found"
        $Team = $Team | Where-Object DisplayName -EQ "$String"
      }

      if ( $Team.Count -gt 1 ) {
        Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but not unique!"
        throw [System.Reflection.AmbiguousMatchException]::New('Multiple Targets found - Result not unique (Team)')
      }

      Write-Verbose -Message "Returning Team with DisplayName: '$($Team.DisplayName)'"
      Write-Output $Team
    }
    else {
      throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - No Team found for '$TeamId'."
    }

  }
  catch {
    throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - Team not found. Exception: $($_.Exception.Message)"
  }

  # Finding and returning Channel
  try {
    $Channel = $null
    if ($ChannelId -match $TFMatchChannelGuid) {
      Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Channel with Id: '$ChannelId'"
      $Channel = Get-TeamChannel -GroupId $Team.GroupId | Where-Object Id -EQ $ChannelId -ErrorAction Stop
    }
    else {
      Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Channel with DisplayName: '$ChannelId'"
      $Channel = Get-TeamChannel -GroupId $Team.GroupId | Where-Object DisplayName -EQ "$ChannelId" -ErrorAction Stop
    }

    if ( $Channel ) {
      if ($PSBoundParameters.ContainsKey('Debug') -or $DebugPreference -eq 'Continue') {
        " Function: $($MyInvocation.MyCommand.Name) - Channel:", ($Channel | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
      }

      # dealing with potential duplicates
      if ( $Team.Count -gt 1 ) {
        Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but multiple Channels found"
        $Team = $Team | Where-Object DisplayName -EQ "$String"
      }
      if ( $Team.Count -gt 1 ) {
        Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but not unique!"
        throw [System.Reflection.AmbiguousMatchException]::New('Multiple Targets found - Result not unique (Channel)')
      }

      Write-Verbose -Message "$($MyInvocation.MyCommand) - Returning Channel with DisplayName: '$($Channel.DisplayName)'"
      Write-Output $Channel
    }
    else {
      throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - Team found, but no Channel found with this Name/Guid: '$ChannelId'"
    }
  }
  catch {
    throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - Channel not found. Exception: $($_.Exception.Message)"
  }
} #Get-TeamAndChannel