Private/Get-UserLicensesFromLicenseDetailObject.ps1

# Module: TeamsFunctions
# Function: Licensing
# Author: David Eberhardt
# Updated: 16-JUL-2022
# Status: RC




function Get-UserLicensesFromLicenseDetailObject {
  <#
    .SYNOPSIS
        Transforms a AzureAdUserLicenseDetail Object to a License Object
    .DESCRIPTION
    Used by Get-AzureAdUserLicense and Get-AzureAdUserLicenseServicePlan as well as Get-TeamsUserVoiceConfig
  .PARAMETER String
    String in on of the formats:
    TeamId\ChannelId, TeamId\ChannelDisplayName, TeamDisplayName,ChannelId or TeamDisplayName\ChannelDisplayName
    .EXAMPLE
    $UserLicenseDetail = Get-AzureADUserLicenseDetail -ObjectId "$UserPrincipalName"
    $UserLicenses = Get-UserLicensesFromLicenseDetailObject -UserLicenseDetail $UserLicenseDetail
 
    Populates the Variable UserLicenseDetail and transforms it into a usable License Object for further processing.
  .NOTES
    This helper function is targeted by TeamsFunctions CmdLets that require the Licenses assigned to a User
  #>


  [CmdletBinding()]
  param(
    [Parameter(Mandatory, ValueFromPipeline)]
    [object]$UserLicenseDetail
  ) #param

  begin {
    Show-FunctionStatus -Level Live
    $Stack = Get-PSCallStack
    $Called = ($stack.length -ge 3)
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    # Loading License Array
    if (-not $global:TeamsFunctionsMSAzureAdLicenses) {
      $global:TeamsFunctionsMSAzureAdLicenses = Get-AzureAdLicense -WarningAction SilentlyContinue
    }

    $AllLicenses = $null
    $AllLicenses = $global:TeamsFunctionsMSAzureAdLicenses

  } #begin

  process {

    $assignedSkuPartNumbers = $UserLicenseDetail.SkuPartNumber
    [System.Collections.Generic.List[object]]$UserLicenses = @()
    foreach ($PartNumber in $assignedSkuPartNumbers) {
      $Lic = $null
      $Lic = $AllLicenses | Where-Object SkuPartNumber -EQ $Partnumber
      if ($null -ne $Lic) {
        if ($PSBoundParameters.ContainsKey('Debug') -or $DebugPreference -eq 'Continue') {
          " Function: $($MyInvocation.MyCommand.Name) - License:", ($Lic | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
        }

        [void]$UserLicenses.Add($Lic)
      }
    }
    Write-Output $UserLicenses | Sort-Object ProductName
  } #process

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