Public/Helper/Get-GraphUserLicensesFromLicenseDetailObject.ps1

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

#VALIDATE whether MgUser Parameter AssignedPlans and AssignedLicenses would be enough for this!


function Get-GraphUserLicensesFromLicenseDetailObject {
  <#
    .SYNOPSIS
        Transforms a MgUserLicenseDetail Object to a License Object
    .DESCRIPTION
    Used by Cmdlets to reformat a License Object
  .PARAMETER UserLicenseDetail
    MgUserLicenseDetail
    .EXAMPLE
    $UserLicenseDetail = Get-MgUserLicenseDetail -Id "$UserPrincipalName"
    $UserLicenses = Get-GraphUserLicensesFromLicenseDetailObject -UserLicenseDetail $UserLicenseDetail
 
    Populates the Variable UserLicenseDetail and transforms it into a usable License Object for further processing.
  .NOTES
    This helper function is targeted by Orbit CmdLets that require the Licenses assigned to a User
  .FUNCTIONALITY
    Extracts User assigned Licenses from a License Detail Object
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Users/Get-GraphUserLicensesFromLicenseDetailObject.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  #>


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

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

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

    $AllLicenses = $null
    $AllLicenses = $global:OrbitMicrosoft365Licenses

  } #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['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-GraphUserLicensesFromLicenseDetailObject