Private/Get-UserServicePlansFromLicenseDetailObject.ps1

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




function Get-UserServicePlansFromLicenseDetailObject {
  <#
    .SYNOPSIS
        Transforms a AzureAdUserLicenseDetail Object to a ServicePlan Object
    .DESCRIPTION
    Used by Get-AzureAdUserLicense and Get-AzureAdUserLicenseServicePlan
  .PARAMETER String
    String in on of the formats:
    TeamId\ChannelId, TeamId\ChannelDisplayName, TeamDisplayName,ChannelId or TeamDisplayName\ChannelDisplayName
    .EXAMPLE
    $UserLicenseDetail = Get-AzureADUserLicenseDetail -ObjectId "$UserPrincipalName"
    $UserServicePlans = Get-UserServicePlansFromLicenseDetailObject -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 ServicePlans 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:TeamsFunctionsMSAzureAdLicenseServicePlans) {
      $global:TeamsFunctionsMSAzureAdLicenseServicePlans = Get-AzureAdLicenseServicePlan -WarningAction SilentlyContinue
    }

    $AllServicePlans = $null
    $AllServicePlans = $global:TeamsFunctionsMSAzureAdLicenseServicePlans

  } #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.ContainsKey('Debug') -or $DebugPreference -eq 'Continue') {
          " Function: $($MyInvocation.MyCommand.Name) - License:", ($Lic | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
        }
        if ($PSBoundParameters.ContainsKey('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.AzureAdUserLicense.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-UserServicePlansFromLicenseDetailObject