Public/GraphUserLicense/Get-GraphUserLicense.ps1
# Module: Orbit.Users # Function: Licensing # Author: David Eberhardt # Updated: 01-APR-2020 # Status: Live #TODO Refactor cmdlet name to GraphUserLicense, add Alias to Get-GraphUserLicense and Get-GraphUserLicense #TODO Repeat for all TeamsUserLicense Cmdlets function Get-GraphUserLicense { <# .SYNOPSIS Returns Teams License information for an Object in Graph User .DESCRIPTION Returns an Object containing all Teams related Licenses found for a specific Object Licenses and ServicePlans are nested in the respective parameters for further investigation .PARAMETER UserPrincipalName The UserPrincipalName, ObjectId or Identity of the Object. .PARAMETER DisplayAll Displays all Licenses, not only identified or relevant Teams Licenses .EXAMPLE Get-GraphUserLicense [-UserPrincipalName] John@domain.com Displays all licenses assigned to User John@domain.com .EXAMPLE Get-GraphUserLicense -UserPrincipalName John@domain.com,Jane@domain.com Displays all licenses assigned to Users John@domain.com and Jane@domain.com .EXAMPLE Import-Csv User.csv | Get-GraphUserLicense Displays all licenses assigned to Users from User.csv, Column UserPrincipalName. The input file must have a single column heading of "UserPrincipalName" with properly formatted UPNs. .INPUTS System.String .OUTPUTS System.Object .NOTES Requires a connection to Azure Active Directory .COMPONENT Licensing .FUNCTIONALITY Returns a list of Licenses assigned to a specific User depending on input .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Users/Get-GraphUserLicense.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Licensing.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_UserManagement.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/ #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Justification = 'Required for performance. Removed with Disconnect-Me')] [CmdletBinding()] [Alias('Get-TeamsUserLicense')] [OutputType([PSCustomObject])] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, HelpMessage = 'Enter the UPN or login name of the user account, typically <user>@<domain>.')] [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, [Parameter(HelpMessage = 'Displays all Licenses, not only Teams relevant')] [switch]$DisplayAll ) #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' } # 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' } # preparing Output Field Separator $OFS = ', ' # do not remove - Automatic variable, used to separate elements! if ($PSBoundParameters['DisplayAll']) { $previousFEL = $global:FormatEnumerationLimit $global:FormatEnumerationLimit = -1 } } #begin process { Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)" foreach ($User in $UserPrincipalName) { try { $UserObject = Get-MgUser -UserId "$User" -WarningAction SilentlyContinue -ErrorAction STOP $UserLicenseDetail = Get-MgUserLicenseDetail -UserId "$User" -WarningAction SilentlyContinue -ErrorAction STOP } catch { if ($_.Exception.Message.Contains('does not exist')) { Write-Error -Message "User '$User' - Account not valid" -Category ObjectNotFound -RecommendedAction 'Verify UserPrincipalName' } else { Write-Error "User '$User' - Exception Message: $($_.Exception.Message)" } continue } [string]$DisplayName = $UserObject.DisplayName if ( $UserLicenseDetail ) { # Querying Licenses $UserLicenses = Get-GraphUserLicensesFromLicenseDetailObject -UserLicenseDetail $UserLicenseDetail @args # Querying Service Plans $UserServicePlans = Get-GraphUserServicePlansFromLicenseDetailObject -UserLicenseDetail $UserLicenseDetail @args } # Filter if ( -not $DisplayAll.IsPresent ) { $UserLicenses = $UserLicenses | Where-Object { $_.IncludesTeams -or $_.IncludesPhoneSystem } $UserServicePlans = $UserServicePlans | Where-Object RelevantForTeams } # Determining Phone System Status $PhoneSystemLicense = ('MCOEV' -in $UserServicePlans.ServicePlanName) $PhoneSystemVirtual = ('MCOEV_VIRTUALUSER' -in $UserServicePlans.ServicePlanName) if ( $PhoneSystemLicense ) { $PhoneSystemProvisioningStatus = $UserServicePlans | Where-Object ServicePlanName -EQ 'MCOEV' if ( $PhoneSystemProvisioningStatus.Count -gt 1 ) { # PhoneSystem assigned more than once! Write-Warning -Message "User '$User' Multiple assignments found for PhoneSystem. Please verify License assignment." $PhoneSystemStatus = ($PhoneSystemProvisioningStatus | Select-Object -ExpandProperty ProvisioningStatus) -join ', ' } else { $PhoneSystemStatus = $PhoneSystemProvisioningStatus.ProvisioningStatus } } elseif ( $PhoneSystemVirtual ) { $PhoneSystemStatus = ($UserServicePlans | Where-Object ServicePlanName -EQ 'MCOEV_VIRTUALUSER').ProvisioningStatus } else { $PhoneSystemStatus = 'Unassigned' } # Calling Plans $currentCallingPlan = ($UserLicenses | Where-Object LicenseType -EQ 'CallingPlan').ProductName $output = [PSCustomObject][ordered]@{ PSTypeName = 'PowerShell.TeamsFunctsions.GraphUserLicense' UserPrincipalName = $User DisplayName = $DisplayName ObjectId = $UserObject.ObjectId UsageLocation = $UserObject.UsageLocation Licenses = $UserLicenses ServicePlans = $UserServicePlans AudioConferencing = $('MCOMEETADV' -in $UserServicePlans.ServicePlanName) CommonAreaPhoneLicense = $('MCOCAP' -in $UserLicenses.SkuPartNumber) PhoneSystemVirtualUser = $PhoneSystemVirtual PhoneSystem = $PhoneSystemLicense PhoneSystemStatus = $PhoneSystemStatus CallingPlanDomestic120 = $('MCOPSTN5' -in $UserServicePlans.ServicePlanName -or 'MCOPSTN_5' -in $UserServicePlans.ServicePlanName) CallingPlanDomestic = $('MCOPSTN1' -in $UserServicePlans.ServicePlanName -or 'MCOPSTN_1' -in $UserServicePlans.ServicePlanName) # also covering MCOPSTN_1_GOV CallingPlanInternational = $('MCOPSTN2' -in $UserServicePlans.ServicePlanName -or 'MCOPSTNEAU2' -in $UserServicePlans.ServicePlanName) OtherCallingPlan = $('MCOPSTNEAU2' -in $UserServicePlans.ServicePlanName -or 'BUSINESS_VOICE_MED2_TELCO' -in $UserServicePlans.ServicePlanName) CommunicationsCredits = $('MCOPSTNC' -in $UserServicePlans.ServicePlanName) CallingPlan = $currentCallingPlan } #Adding Script Method to Licenses and ServicePlans if ( $output.Licenses ) { $output.Licenses | Add-Member -MemberType ScriptMethod -Name ToString -Value { $this.ProductName } -Force } if ( $output.ServicePlans ) { $output.ServicePlans | Add-Member -MemberType ScriptMethod -Name ToString -Value { $this.ProductName } -Force } Write-Output $output } } #process end { if ($PSBoundParameters['DisplayAll']) { $global:FormatEnumerationLimit = $previousFEL } Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)" } #end } #Get-GraphUserLicense |