Functions/Public/Get-OBNLicense.ps1
function Get-OBNLicense { <# .SYNOPSIS Get Office 365 license information .DESCRIPTION Get a list of how many Office 365 licenses are in use for a spcific tenant or get details of who is using each license. .PARAMETER Mode Mode determines which details are returned. Summary : How many licenses are active/in-use Detail : Who is using each in-use license .EXAMPLE Get-OBNLicense Get a summary of how many licenses are active/in-use .EXAMPLE Get-OBNLicense -Mode Summary Get a summary of how many licenses are active/in-use .EXAMPLE Get-OBNLicense -Mode Detail Get a list of who is using each in-use license .EXAMPLE Get-OBNLicense -Mode Detail -Csv onebyte Get a list of who is using each in-use license. The list will be converted to CSV and saved in the file 'onebyte.csv' in the current directory. .NOTES Authored By: Simon Buckner Email: simon@onebyte.net Date: 7th August 2020 To Do: - Make use of delegated admin to get list of tenant organisations allowing script to run across multipe environments .LINK https://github.com/simononebyte/PSOnebyte #> [CmdletBinding(SupportsShouldProcess = $False)] Param( # Switch between providing summary stats or a detailed list [Parameter( Mandatory = $False, Position = 0 )] [ValidateSet("Summary", "Detail")] [String]$Mode = "Summary", # Output to the specified CSV file [Parameter( Mandatory = $false, Position = 1 )] [ValidateLength(1, 256)] [String]$CSV ) BEGIN { # Run one-time set-up tasks here, like defining variables, etc. Set-StrictMode -Version Latest Write-Verbose -Message "$($MyInvocation.MyCommand.Name): Started." # Declare any supporting functions here # ######################################################################### # function myFunc([string]$p) { # Write-Host "myFunc called with" # } # Run any initialisation steps here # ######################################################################### $PLANS = GetResourcePlans } Process { $output = @() switch ($Mode) { "Detail" { Write-Verbose "Getting license details" # Get a list of who is using each in-use license # ######################################################################### $users = @() Get-MsolUser | ForEach-Object { $upn = $_.UserPrincipalName $displayName = $_.DisplayName foreach ($license in $_.Licenses) { $parts = $license.AccountSkuId.Split(":") $id = $parts[$parts.Length - 1] $name = $PLANS[$id] if ($name) { $user = [OBNUser]::New() $user.UPN = $upn $user.DisplayName = $displayName $user.LicenseId = $id $user.LicenseName = $name $users += $user } } } $output = $users | Sort-Object -Property "DisplayName" } Default { Write-Verbose "Getting license summary" # Get a summary of how many licenses are active/in-use # ######################################################################### $licenses = @() Get-MsolAccountSku | ForEach-Object { $parts = $_.AccountSkuId.Split(":") $id = $parts[$parts.Length - 1] $name = $PLANS[$id] if ($name) { $lic = [OBNLicense]::New() $lic.Id = $id $lic.Name = $name $lic.Active = $_.ActiveUnits $lic.Used = $_.ConsumedUnits $licenses += $lic } } $output = $licenses | Sort-Object -Property "Name" } } if ($CSV -eq "") { Write-Verbose "Sending results to the output pipe" $output } else { if ($CSV -notmatch "\.csv$") { $CSV = "$CSV.csv" } Write-Verbose "Sending results to CSV file $CSV" $output | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath $CSV } } END { # Finally, run one-time tear-down tasks here. Write-Verbose -Message "$($MyInvocation.MyCommand.Name): Complete." } } |