Private/New-GraphLicenseObject.ps1

# Module: Orbit.Users
# Function: Graph Licensing
# Author: David Eberhardt
# Updated: 01-SEP-2020
# Status: Live

#Set to Private Function as no longer needed. to be removed later!


function New-GraphLicenseObject {
  <#
  .SYNOPSIS
    Creates a new License Object for processing
  .DESCRIPTION
    Helper function to create a new License Object
  .PARAMETER SkuId
    SkuId(s) of the License to be added
  .PARAMETER RemoveSkuId
    SkuId(s) of the License to be removed
  .EXAMPLE
    New-GraphLicenseObject -SkuId e43b5b99-8dfb-405f-9987-dc307f34bcbd
 
    Will create a license Object for the MCOEV license .
  .EXAMPLE
    New-GraphLicenseObject -SkuId e43b5b99-8dfb-405f-9987-dc307f34bcbd -RemoveSkuId 440eaaa8-b3e0-484b-a8be-62870b9ba70a
 
    Will create a license Object based on the existing users License
    Adding the MCOEV license, removing the MCOEV_VIRTUALUSER license.
  .INPUTS
    System.String
  .OUTPUTS
    PSCustomObject
  .NOTES
    This function does not require any connections to Graph.
    However, applying the output of this Function does.
    Used in Set-GraphUserLicense and Add-TeamsUserLicense
  .COMPONENT
    SupportingFunction
    Licensing
  .FUNCTIONALITY
    Creates a new License Object to be applied with Set-GraphUserLicense
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Users/New-GraphLicenseObject.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('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Object created locally for passing to other command. No impact')]
  [CmdletBinding(ConfirmImpact = 'Low')]
  [OutputType([PSCustomObject])] #LicenseObject
  param(
    [Parameter(Mandatory = $false, Position = 0, HelpMessage = 'SkuId of the license to Add')]
    [Alias('AddSkuId')]
    [string[]]$SkuId,

    [Parameter(Mandatory = $false, Position = 1, HelpMessage = 'SkuId of the license to Remove')]
    [string[]]$RemoveSkuId
  ) #param

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

    # Setting Preference Variables according to Upstream settings
    if (-not $PSBoundParameters['Verbose']) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') }
    if (-not $PSBoundParameters['Confirm']) { $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference') }
    if (-not $PSBoundParameters['WhatIf']) { $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference') }
    $DebugPreference = if (-not $PSBoundParameters['Debug']) { $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } else { 'Continue' }
    $InformationPreference = if ( $PSBoundParameters['InformationAction']) { $PSCmdlet.SessionState.PSVariable.GetValue('InformationAction') } else { 'Continue' }

  } #begin

  process {
    #Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"
    $newLicensesObj = @{}

    # Creating AddLicenses
    if ($PSBoundParameters['SkuId']) {
      foreach ($Sku in $SkuId) {
        $AddLicenseObj = New-Object -TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphAssignedLicense
        $AddLicenseObj.SkuId += $Sku

        $newLicensesObj.AddLicenses += $AddLicenseObj
      }
    }
    else {
      $newLicensesObj.AddLicenses = @()
    }

    # Creating RemoveLicenses
    if ($PSBoundParameters['RemoveSkuId']) {
      $newLicensesObj.RemoveLicenses = $RemoveSkuId

    }

    return $newLicensesObj
  } #process

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