Public/Helper/Get-GraphUserServicePlansFromLicenseDetailObject.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-GraphUserServicePlansFromLicenseDetailObject {
  <#
    .SYNOPSIS
        Transforms a MgUserLicenseDetail Object to a ServicePlan Object
    .DESCRIPTION
    Used by Cmdlets to reformat a License Object
  .PARAMETER UserLicenseDetail
    MgUserLicenseDetail
    .EXAMPLE
    $UserLicenseDetail = Get-MgUserLicenseDetail -Id "$UserPrincipalName"
    $UserServicePlans = Get-GraphUserServicePlansFromLicenseDetailObject -UserLicenseDetail $UserLicenseDetail
 
    Populates the Variable UserLicenseDetail and transforms it into a usable License Object for further processing.
  .NOTES
    This helper function is targeted by CmdLets that require the ServicePlans assigned to a User
  .COMPONENT
    SupportingFunction
    Licensing
  .FUNCTIONALITY
    Extracts User assigned ServicePlans from a License Detail Object
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Users/Get-GraphUserServicePlansFromLicenseDetailObject.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-FunctionStatus -Level Live
    $Stack = Get-PSCallStack
    $Called = ($stack.length -ge 3)
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

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

    $AllServicePlans = $null
    $AllServicePlans = $global:OrbitMicrosoft365LicenseservicePlans

  } #begin

  process {

    $assignedServicePlans = $UserLicenseDetail.ServicePlans | Sort-Object ServicePlanName
    [System.Collections.Generic.List[object]]$UserServicePlans = @()
    foreach ($ServicePlan in $assignedServicePlans) {
      $Lic = $null
      $Lic = $AllServicePlans | Where-Object ServicePlanName -EQ $ServicePlan.ServicePlanName
      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
        }
        if ($PSBoundParameters['Debug'] -or $DebugPreference -eq 'Continue') {
          " Function: $($MyInvocation.MyCommand.Name) - ServicePlan:", ($ServicePlan | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
        }

        $LicObj = [PSCustomObject][ordered]@{
          PSTypeName         = 'PowerShell.TeamsFunctsions.GraphUserLicense.ServicePlan'
          ProductName        = if ($Lic.ProductName) { $Lic.ProductName } else { $ServicePlan.ServicePlanName }
          ServicePlanName    = $ServicePlan.ServicePlanName
          ProvisioningStatus = $ServicePlan.ProvisioningStatus
          RelevantForTeams   = $Lic.RelevantForTeams
        }
        [void]$UserServicePlans.Add($LicObj)
      }
    }
    Write-Output $UserServicePlans | Sort-Object ProductName, ProvisioningStatus, ServicePlanName

  } #process

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