Public/Support/Get-TeamsObjectType.ps1

# Module: Orbit.Teams
# Function: AutoAttendant
# Author: David Eberhardt
# Updated: 01-JAN-2021
# Status: Live




function Get-TeamsObjectType {
  <#
  .SYNOPSIS
    Resolves the type of the object
  .DESCRIPTION
    Helper function to find the Callable Entity Type of Teams Objects
    Returns ObjectType: User (GraphUser), Group (GraphGroup), ResourceAccount (ApplicationInstance) or TelURI String (ExternalPstn)
  .PARAMETER Identity
    Required. String for the TelURI, Group Name or Mailnickname, UserPrincipalName, depending on the Entity Type
  .EXAMPLE
    Get-TeamsObjectType -Identity "John@domain.com"
 
    Returns "User" as the type of Entity if an GraphUser with the UPN "John@domain.com" is found
  .EXAMPLE
    Get-TeamsObjectType -Identity "Accounting"
 
    Returns "Group" as the type of Entity if a GraphGroup with the Name "Accounting" is found.
  .EXAMPLE
    Get-TeamsObjectType -Identity "Accounting@domain.com"
 
    Returns "Group" as the type of Entity if a GraphGroup with the Mailnickname "Accounting@domain.com" is found.
  .EXAMPLE
    Get-TeamsObjectType -Identity "ResourceAccount@domain.com"
 
    Returns "ResourceAccount" as the type of Entity if a CsOnlineApplicationInstance with the UPN "ResourceAccount@domain.com" is found
  .EXAMPLE
    Get-TeamsObjectType -Identity "tel:+1555123456"
 
    Returns "TelURI" as the type of Entity
  .EXAMPLE
    Get-TeamsObjectType -Identity "+1555123456"
 
    Returns an Error as the type of Entity cannot be determined correctly
  .INPUTS
    System.String
  .OUTPUTS
    System.String
  .NOTES
    None
  .COMPONENT
    UserManagement
    TeamsAutoAttendant
    TeamsCallQueue
  .FUNCTIONALITY
    Determining the Object Type for the String
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Teams/Get-TeamsObjectType.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_UserManagement.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  #>


  [CmdletBinding(ConfirmImpact = 'Low')]
  [OutputType([System.String])]
  param(
    [Parameter(Mandatory, Position = 0, ValueFromPipeline, HelpMessage = 'Identity of the Call Target')]
    [string[]]$Identity
  ) #param

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

    # Asserting Graph Connection
    if ( -not (Test-GraphConnection) ) { throw 'Connection to Microsoft Graph not established. Please validate connection' }

    # Asserting MicrosoftTeams Connection
    if ( -not (Assert-MicrosoftTeamsConnection) ) { throw 'Connection to Microsoft Teams not established. Please validate connection' }

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

  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"
    foreach ($Id in $Identity) {
      if ($script:OrbitRegexPhoneNumber.isMatch($Id) -and -not $script:OrbitRegexUPN.isMatch($Id) ) {
        Write-Verbose -Message "Callable Entity - Call Target '$Id' found: TelURI (ExternalPstn)"
        return 'TelURI'
      }
      elseif ( $script:OrbitRegexChannelGuid.isMatch($Id) ) {
        Write-Verbose -Message "Callable Entity - Call Target '$Id' found: Channel (Channel)"
        return 'Channel'
      }
      else {
        try {
          $User = Get-MgUser -UserId "$Id" -WarningAction SilentlyContinue -ErrorAction Stop
          if ( $User ) {
            if ($User[0].Department -eq 'Microsoft Communication Application Instance') {
              #if ( Test-TeamsResourceAccount $Id ) {
              Write-Verbose -Message "Callable Entity - Call Target '$Id' found: ResourceAccount (ApplicationEndpoint), (VoiceApp)"
              return 'ResourceAccount'
            }
            else {
              Write-Verbose -Message "Callable Entity - Call Target '$Id' found: User (Forward, Voicemail)"
              return 'User'
            }
          }
        }
        catch {
          Write-Verbose -Message "Callable Entity - Call Target '$Id' is not a TelUri, Channel, User (Forward, Voicemail), ResourceAccount - Trying to find an GraphGroup"
        }
      }

      # Last resort - Try GraphGroup
      if ( Test-GraphGroup $Id ) {
        Write-Verbose -Message "Callable Entity - Call Target '$Id' found: Group (SharedVoicemail)"
        return 'Group'
      }
      else {
        # Catch neither
        Write-Verbose -Message 'Callable Entity - ObjectType cannot be determined. (Neither TelURI, nor Channel, GraphUser, ResourceAccount or Group)'
        return 'Unknown'
      }
    }
  }

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