Private/Convert-CallableEntityFriendlyNameToGuid.ps1

# Module: TeamsFunctions
# Function: Assertion
# Author: David Eberhardt
# Updated: 19-JUL-2022
# Status: RC




function Convert-CallableEntityFriendlyNameToGuid {
  <#
    .SYNOPSIS
        Queries Team and Channel based on input
    .DESCRIPTION
    Used by Get-TeamsCallableEntity
  .PARAMETER FriendlyName
    UserPrincipalName of a User or Resource Account or DisplayName of a Office 365 Group
  .PARAMETER CallTarget
    CallableEntityObject to be passed to this object
  .PARAMETER CallTargetType
    One of Group, User, ResourceAccount, ResourceAccountsForCallerId, AuthorizedUsers, ChannelOwner
  .EXAMPLE
    Convert-CallableEntityFriendlyNameToGuid -FriendlyName "My DistributionList" -CallTargetType Group
 
    Returns Guid of the Object if the Object exists
  .EXAMPLE
    Convert-CallableEntityFriendlyNameToGuid -FriendlyName "John@domain.com" -CallTargetType User
 
    Returns Guid of the Object if the Object exists and the User could be ascertained to be a viable CallableEntity
  .EXAMPLE
    Convert-CallableEntityFriendlyNameToGuid -CallTarget $CallableEntityObject -CallTargetType User
 
    Returns Guid of the Object if the Object exists and the Object could be ascertained to be a viable CallableEntity
  .NOTES
    This helper function is targeted by New-TeamsCallQueue and Set-TeamsCallQueue
  #>


  [CmdletBinding(DefaultParameterSetName = 'Name')]
  [OutputType([System.Void], [System.Guid])]
  param (
    [Parameter(ParameterSetName = 'Name', HelpMessage = 'UserPrincipalName of a User or Resource Account or DisplayName of a Office 365 Group')]
    [string]$FriendlyName,

    [Parameter(ParameterSetName = 'Target', HelpMessage = 'CallableEntityObject')]
    [object]$CallTarget,

    [Parameter(HelpMessage = 'UPN of one or more Users')]
    [ValidateSet('Group', 'User', 'ResourceAccount', 'ResourceAccountsForCallerId', 'AuthorizedUsers', 'ChannelOwner') ]
    [string]$CallTargetType
  )

  begin {
    #Show-FunctionStatus -Level RC

    # preparing Splatting Object
    $Parameters = $null

    # Assertion is done depending on the CallTargetType
    $AssertObject = $true # for all except DistributionLists
  }

  process {
    if ( $PSCmdlet.ParameterSetName -eq 'Name') {
      # Finding Callable Entity
      $CallTarget = $null
      $CallTarget = Get-TeamsCallableEntity -Identity "$FriendlyName"
    }

    if ( $CallTarget.ObjectType -eq 'Unknown') {
      Write-Warning -Message "Object '$FriendlyName' is Unknown, omitting Object!"
      continue
    }
    elseif ( $CallTarget.ObjectType -ne $CallTargetType) {
      Write-Warning -Message "Object '$FriendlyName' is not of type $CallTargetType, omitting Object!"
      continue
    }
    elseif ( $CallTarget.ObjectType -eq 'Group' ) {
      $AssertObject = $false
      if ($CallTarget) {
        Write-Information "INFO: $CallTargetType '$FriendlyName' will be considered"
        #TODO Expand validation here? Test whether Users in DL are enabled for EV and/or licensed?
        return $CallTarget.Identity
      }
      else {
        Write-Warning -Message "Object '$FriendlyName' not found or in unusable state, omitting Object!"
        continue
      }
    }
    else {
      # Assert and test
      switch ($CallTargetType) {
        'User' {
          # Users require Enterprise Voice to be enabled in order to be considered Agents in a Queue or a Callable Entity
          $Parameters += @{ 'RequireEV' = $true }
          $AssertObject = $true
        }
        'ResourceAccount' {
          # Resource Account does not need Enterprise Voice by default (will not be enabled if no phone number is assigned)
          $AssertObject = $true
        }
        'ResourceAccountsForCallerId' {
          # Resource Account used as a CallerId requires a phone Number and therefore will be enabled for Enterprise Voice
          $Parameters += @{ 'RequireEV' = $true }
          $AssertObject = $true
        }
        'AuthorizedUsers' {
          # Authorized Users are currently unknown whether they require any specific qualifications before they can be added
          #TODO Acquire more information regarding this Type
          $AssertObject = $false
        }
        'ChannelOwner' {
          $AssertObject = $false
        }
      }

      # Asserting Object
      if ( $AssertObject -and $CallTarget ) {
        try {
          $Assertion = $null
          $Parameters += @{ 'WarningAction' = 'SilentlyContinue' }
          $Parameters += @{ 'ErrorAction' = 'Stop' }
          $Parameters += @{ 'UserPrincipalName' = "$($CallTarget.Entity)" }
          $Assertion = Assert-TeamsCallableEntity @Parameters @args
          if ( $Assertion ) {
            Write-Information "INFO: $CallTargetType '$FriendlyName' will be considered"
            return $CallTarget.Identity
          }
          else {
            Write-Warning -Message "Object '$FriendlyName' not found or in unusable state, omitting Object!"
            continue
          }
        }
        catch {
          Write-Warning -Message "Object '$FriendlyName' not in correct state or not enabled for Enterprise Voice, omitting Object!"
          Write-Debug "Exception: $($_.Exception.Message)"
          continue
        }
      }
    }
  }
  end {

  }
} # Convert-CallableEntityFriendlyNameToGuid