Functions/Get-Microsoft365LicenseInformation.ps1
<#
.SYNOPSIS This function returns objects containing the product name, string ID and GUID of the various Microsoft 365 licenses. It retrieves this information by parsing the values at https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/licensing-service-plan-reference. #> function Get-Microsoft365LicenseInformation { [CmdletBinding(PositionalBinding=$false)] [OutputType([PSCustomObject[]])] param ( # Select the stream where the error messages will be directed. [Parameter(Mandatory=$false)] [ValidateSet("Information", "Warning", "Error", "None")] [String]$outputStream = "Error" ) # Retrieve the raw HTML of the page try { $pageHtml = Invoke-WebRequest -Uri "https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/licensing-service-plan-reference" -UseBasicParsing } catch { Write-OutputMessage "Exception occurred while retrieving the raw HTML of the page.`r`n$($.Exception.Message)" return } # Extract the body portion of the table $pageHtml -match "<tbody>([\s\S]*?)</tbody>" | Out-Null $tableBody = $Matches[1] # Retrieve all the descriptions for each of the licenses $licenseDescriptions = ([Regex]"<tr>([\s\S]*?)</tr>").Matches($tableBody) | ForEach-Object -Process { $_.Groups[1].Value } # Generate the license information objects $licenseInformationObjects = @() foreach ($description in $licenseDescriptions) { $licenseInformationObject = [PSCustomObject]@{} $description = $description.Trim() $splitDescription = $description.Split($LINE_FEED) $licenseInformationObject | Add-Member -NotePropertyName "ProductName" -NotePropertyValue ([Regex]"<td>(.*)</td>").Match($splitDescription[0]).Groups[1].Value $licenseInformationObject | Add-Member -NotePropertyName "SkuPartNumber" -NotePropertyValue ([Regex]"<td>(.*)</td>").Match($splitDescription[1]).Groups[1].Value $licenseInformationObject | Add-Member -NotePropertyName "GUID" -NotePropertyValue ([Regex]"<td>(.*)</td>").Match($splitDescription[2]).Groups[1].Value $licenseInformationObjects += $licenseInformationObject } return $licenseInformationObjects } |