Public/GraphUserLicense/Test-GraphUserHasCallPlan.ps1

# Module: Orbit.Users
# Function: Testing
# Author: David Eberhardt
# Updated: 01-OCT-2020
# Status: Live




function Test-GraphUserHasCallPlan {
  <#
  .SYNOPSIS
    Tests an Graph User Object for a CallingPlan License
  .DESCRIPTION
    Any assigned Calling Plan found on the User (with exception of the Communication Credits license, which is add-on)
    will let this function return $TRUE
  .PARAMETER Identity
    Mandatory. The sign-in address or User Principal Name of the user account to modify.
  .EXAMPLE
    Test-GraphUserHasCallPlan -Identity User@domain.com
 
    Will Return $TRUE only if one of the following license Packages are assigned:
    InternationalCallingPlan, DomesticCallingPlan, DomesticCallingPlan120, DomesticCallingPlan120b
  .INPUTS
    System.String
  .OUTPUTS
    Boolean
  .NOTES
    This Script is indiscriminate against the User Type, all Graph User Objects can be tested.
  .FUNCTIONALITY
    Returns a boolean value for when any of the Calling Plan licenses are found assigned to a specific user.
  .COMPONENT
    SupportingFunction
    Licensing
  .FUNCTIONALITY
    Tests whether the User has a Microsoft Calling Plan License
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Users/Test-GraphUserHasCallPlan.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  #>


  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Justification = 'Required for performance. Removed with Disconnect-Me')]
  [CmdletBinding()]
  [Alias('Test-TeamsUserHasCallPlan')]
  [OutputType([Boolean])]
  param(
    [Parameter(Mandatory, Position = 0, ValueFromPipeline, HelpMessage = 'This is the UserID (UPN)')]
    [Alias('ObjectId', 'Identity')]
    [ValidateScript( {
        If ($script:OrbitRegexUPN.isMatch($_) -or $script:OrbitRegexGuid.isMatch($_)) { $True } else {
          throw [System.Management.Automation.ValidationMetadataException] 'Value must be a valid UPN or ObjectId'
        } })]
    [string]$UserPrincipalName
  ) #param

  begin {
    Show-OrbitFunctionStatus -Level Live
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    # Asserting Graph Connection
    if ( -not (Test-GraphConnection) ) { throw 'Connection to Microsoft Graph not established. Please validate connection' }

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

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

  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"
    foreach ($ID in $UserPrincipalName) {
      # Query User
      try {
        $UserObject = Get-MgUser -UserId "$ID" -WarningAction SilentlyContinue -ErrorAction Stop
        $UserLicenseObject = Get-MgUserLicenseDetail -UserId $($UserObject.ObjectId) -WarningAction SilentlyContinue
        $UserLicenseSKU = $UserLicenseObject.SkuPartNumber
        if ($PSBoundParameters['Debug']) {
          " Function: $($MyInvocation.MyCommand.Name) - UserLicenseSKU:", ($UserLicenseSKU | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
        }
      }
      catch {
        [string]$Message = $_ | Get-ErrorMessageFromErrorString
        Write-Warning -Message "User '$ID': GetUser$($Message.Split(':')[1])"
      }

      $CallingPlanLicenses = $AllLicenses | Where-Object { $_.Serviceplans.ServicePlanName -match 'MCOPSTN' }
      if ($PSBoundParameters['Debug']) {
        " Function: $($MyInvocation.MyCommand.Name) - CallingPlanLicenses:", ($CallingPlanLicenses | Format-Table -AutoSize | Out-String).Trim() | Write-Debug
      }
      $HasCallingPlan = $userLicenseSku | ForEach-Object {
        Write-Verbose -Message "Testing '$_'"
        $_ -in $CallingPlanLicenses.SkuPartNumber
      }
      if ( $HasCallingPlan -contains $true ) {
        return $true
      }
      else {
        return $false
      }
    }
  } #process

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