<# .TITLE License Threshold Alert Notification .SYNOPSIS Automated runbook that emails administrators when Intune-capable license SKUs approach exhaustion. .DESCRIPTION This script is designed to run as a scheduled Azure Automation runbook. It reads the tenant's subscribed SKUs, identifies the ones that include an Intune service plan, and compares consumed against purchased units. When utilization crosses the configured threshold (default 90 percent) or a SKU is suspended or in warning state, administrators get an email before new users fail to enroll for lack of a license. Non-Intune SKUs can be included with a switch for a full license overview. .TAGS Notification .MINROLE Intune Administrator .PERMISSIONS Organization.Read.All,Mail.Send .AUTHOR Ugur Koc .VERSION 1.2 .CHANGELOG 1.2 - Added Azure Automation contract validation, portal-safe boolean parameters, beta Graph endpoints, and terminating paging errors 1.1 - Azure Automation now records script progress, outcomes, and summaries in job history 1.0 - Initial release .LASTUPDATE 2026-07-30 .EXECUTION RunbookOnly .OUTPUT Email .SCHEDULE Weekly .CATEGORY Notification .EXAMPLE .\license-threshold-alert.ps1 -EmailRecipients "" -SenderUPN "" Alerts when any Intune-capable SKU passes 90 percent utilization .EXAMPLE .\license-threshold-alert.ps1 -ThresholdPercent 80 -IncludeAllSkus "true" -EmailRecipients "" -SenderUPN "" Alerts at 80 percent utilization across every SKU in the tenant .NOTES - Requires Microsoft.Graph.Authentication module - For Azure Automation, configure Managed Identity with the required permissions - The Automation account's managed identity requires Mail.Send permission for the SenderUPN mailbox - Intune-capable SKUs are detected by an INTUNE_A service plan; trial and locked-out units are not counted as available - subscribedSkus is a v1.0 endpoint and readable with Organization.Read.All - Local interactive sign-in uses the MgGraphCommunity module to avoid the Graph SDK's mandatory WAM broker on Windows #> [CmdletBinding()] param( [Parameter(Mandatory = $true, HelpMessage = "Comma-separated list of email addresses to send notifications")] [ValidateNotNullOrEmpty()] [string]$EmailRecipients, [Parameter(Mandatory = $true, HelpMessage = "Mailbox UPN used as the notification sender (managed identity needs Mail.Send permission for it)")] [ValidateNotNullOrEmpty()] [string]$SenderUPN, [Parameter(Mandatory = $false, HelpMessage = "Utilization percentage that triggers the alert")] [ValidateRange(1, 100)] [int]$ThresholdPercent = 90, [Parameter(Mandatory = $false, HelpMessage = "Include SKUs without an Intune service plan")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$IncludeAllSkus, [Parameter(Mandatory = $false, HelpMessage = "Send the report even when no SKU crosses the threshold")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$AlwaysSend, [Parameter(Mandatory = $false, HelpMessage = "Force module installation without prompting")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$ForceModuleInstall ) # Normalize the local module-install override for Azure Automation parameter binding. $forceModuleInstallRaw = [string]$ForceModuleInstall Remove-Variable -Name ForceModuleInstall if ([string]::IsNullOrWhiteSpace($forceModuleInstallRaw)) { $ForceModuleInstall = $false } elseif ($forceModuleInstallRaw.Trim().ToLowerInvariant() -in @("true", "1", '$true')) { $ForceModuleInstall = $true } elseif ($forceModuleInstallRaw.Trim().ToLowerInvariant() -in @("false", "0", '$false')) { $ForceModuleInstall = $false } else { throw "Parameter 'ForceModuleInstall' accepts only true, false, 1, 0, $true, or $false." } # Azure Automation supplies portal parameter values as strings. Normalize the # public boolean parameters once so local and runbook execution use real booleans. foreach ($runbookBooleanParameter in @('IncludeAllSkus', 'AlwaysSend')) { $runbookBooleanRaw = [string](Get-Variable -Name $runbookBooleanParameter -ValueOnly) Remove-Variable -Name $runbookBooleanParameter if ([string]::IsNullOrWhiteSpace($runbookBooleanRaw)) { Set-Variable -Name $runbookBooleanParameter -Value $false continue } switch ($runbookBooleanRaw.Trim().ToLowerInvariant()) { { $_ -in @("true", "1", '$true') } { Set-Variable -Name $runbookBooleanParameter -Value $true } { $_ -in @("false", "0", '$false') } { Set-Variable -Name $runbookBooleanParameter -Value $false } default { throw "Parameter '$runbookBooleanParameter' accepts only true, false, 1, 0, $true, or $false." } } } # ============================================================================ # ENVIRONMENT DETECTION AND SETUP # ============================================================================ function Initialize-RequiredModule { param( [string[]]$ModuleNames, [bool]$IsAutomationEnvironment, [bool]$ForceInstall = $false ) foreach ($ModuleName in $ModuleNames) { Write-Verbose "Checking module: $ModuleName" $module = Get-Module -ListAvailable -Name $ModuleName | Select-Object -First 1 if (-not $module) { if ($IsAutomationEnvironment) { $errorMessage = @" Module '$ModuleName' is not available in this Azure Automation Account. To resolve this issue: 1. Go to Azure Portal 2. Navigate to your Automation Account 3. Go to 'Modules' > 'Browse Gallery' 4. Search for '$ModuleName' 5. Click 'Import' and wait for installation to complete Required modules for this script: - Microsoft.Graph.Authentication "@ throw $errorMessage } else { Write-Information "Module '$ModuleName' not found. Installing..." -InformationAction Continue if (-not $ForceInstall) { $response = Read-Host "Install module '$ModuleName'? (Y/N)" if ($response -notmatch '^[Yy]') { throw "Module '$ModuleName' is required but installation was declined." } } try { $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") $scope = if ($isAdmin) { "AllUsers" } else { "CurrentUser" } Install-Module -Name $ModuleName -Scope $scope -Force -AllowClobber -Repository PSGallery Write-Information "✓ Successfully installed '$ModuleName'" -InformationAction Continue } catch { throw "Failed to install module '$ModuleName': $($_.Exception.Message)" } } } Import-Module -Name $ModuleName -Force -ErrorAction Stop } } # Detect execution environment $IsAzureAutomation = $null -ne $PSPrivateMetadata.JobId.Guid # Initialize required modules $RequiredModules = @("Microsoft.Graph.Authentication") # MgGraphCommunity gives WAM-free interactive sign-in for local runs if (-not $IsAzureAutomation) { $RequiredModules += "MgGraphCommunity" } try { Initialize-RequiredModule -ModuleNames $RequiredModules -IsAutomationEnvironment $IsAzureAutomation -ForceInstall $ForceModuleInstall Write-Verbose "✓ All required modules are available" } catch { Write-Error "Module initialization failed: $_" exit 1 } # ============================================================================ # AUTHENTICATION # ============================================================================ try { if ($IsAzureAutomation) { Write-Output "Connecting to Microsoft Graph using Managed Identity..." Connect-MgGraph -Identity -NoWelcome -ErrorAction Stop } else { Write-Output "Connecting to Microsoft Graph..." $Scopes = @( "Organization.Read.All", "Mail.Send" ) Connect-MgGraphCommunity -Scopes $Scopes -NoWelcome -ErrorAction Stop } Write-Output "✓ Successfully connected to Microsoft Graph" } catch { Write-Error "Failed to connect to Microsoft Graph: $($_.Exception.Message)" exit 1 } # ============================================================================ # HELPER FUNCTIONS # ============================================================================ function Get-MgGraphAllPage { param( [Parameter(Mandatory = $true)] [string]$Uri, [int]$DelayMs = 100 ) $AllResults = @() $NextLink = $Uri $RequestCount = 0 do { try { if ($RequestCount -gt 0) { Start-Sleep -Milliseconds $DelayMs } $Response = Invoke-MgGraphRequest -Uri $NextLink -Method GET $RequestCount++ if ($null -ne $Response.value) { $AllResults += $Response.value } else { $AllResults += $Response } $NextLink = $Response.'@odata.nextLink' } catch { if ($_.Exception.Message -like "*429*" -or $_.Exception.Message -like "*throttled*") { Write-Information "Rate limit hit, waiting 60 seconds..." -InformationAction Continue Start-Sleep -Seconds 60 continue } throw "Error fetching data from $NextLink : $($_.Exception.Message)" } } while ($NextLink) # Comma keeps PowerShell from unrolling a single-item array on return return , $AllResults } function Send-EmailNotification { [CmdletBinding(SupportsShouldProcess)] param( [string[]]$Recipients, [string]$Subject, [string]$Body ) try { foreach ($Recipient in $Recipients) { $Message = @{ subject = $Subject body = @{ contentType = "HTML" content = $Body } toRecipients = @( @{ emailAddress = @{ address = $Recipient } } ) } $RequestBody = @{ message = $Message } | ConvertTo-Json -Depth 10 if ($PSCmdlet.ShouldProcess($Recipient, "Send Email Notification")) { $Uri = "https://graph.microsoft.com/beta/users/$SenderUPN/sendMail" Invoke-MgGraphRequest -Uri $Uri -Method POST -Body $RequestBody -ContentType "application/json" | Out-Null Write-Information "✓ Email sent to $Recipient via Microsoft Graph" -InformationAction Continue } } return $true } catch { Write-Error "Failed to send email notification: $($_.Exception.Message)" return $false } } # Function creates email content, does not change system state function New-EmailBody { param( [array]$SkuReport, [int]$Threshold ) $rows = "" foreach ($row in ($SkuReport | Sort-Object UtilizationPct -Descending)) { $highlight = if ($row.Flagged) { " style=`"background-color: #fdecea;`"" } else { "" } $rows += @" $([System.Net.WebUtility]::HtmlEncode($row.SkuPartNumber)) $($row.Consumed) $($row.Enabled) $($row.UtilizationPct)% $($row.Available) $([System.Net.WebUtility]::HtmlEncode($row.Health)) "@ } return @"

License Threshold Report

Alert threshold: $Threshold% | Generated: $(Get-Date -Format 'yyyy-MM-dd HH:mm')

$rows
SKUConsumedPurchasedUtilizationAvailableHealth

Generated automatically by the IntuneAutomation license-threshold-alert runbook.

"@ } # ============================================================================ # MAIN SCRIPT LOGIC # ============================================================================ try { Write-Output "Retrieving subscribed SKUs..." $skus = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/subscribedSkus?`$select=skuPartNumber,skuId,consumedUnits,prepaidUnits,servicePlans,capabilityStatus" [System.Collections.Generic.List[Object]]$skuReport = @() foreach ($sku in @($skus)) { # Intune-capable SKUs carry the INTUNE_A user service plan $hasIntunePlan = @($sku.servicePlans | Where-Object { $_.servicePlanName -like "INTUNE_A*" }).Count -gt 0 if (-not $hasIntunePlan -and -not $IncludeAllSkus) { continue } $enabled = [int]$sku.prepaidUnits.enabled $consumed = [int]$sku.consumedUnits $suspended = [int]$sku.prepaidUnits.suspended $warning = [int]$sku.prepaidUnits.warning # SKUs with zero purchasable units (e.g. fully locked-out trials) cannot be utilized if ($enabled -eq 0 -and $consumed -eq 0) { continue } $utilization = if ($enabled -gt 0) { [math]::Round(($consumed / $enabled) * 100, 1) } else { 100 } $available = $enabled - $consumed $health = "OK" if ($suspended -gt 0) { $health = "Suspended units: $suspended" } elseif ($warning -gt 0) { $health = "Units in warning (grace period): $warning" } elseif ($enabled -eq 0) { $health = "No enabled units" } $flagged = ($utilization -ge $ThresholdPercent) -or ($health -ne "OK") $skuReport.Add([PSCustomObject]@{ SkuPartNumber = $sku.skuPartNumber IntuneCapable = $hasIntunePlan Consumed = $consumed Enabled = $enabled Available = $available UtilizationPct = $utilization Health = $health Flagged = $flagged }) } $flaggedSkus = @($skuReport | Where-Object { $_.Flagged }) Write-Output "✓ Analyzed $($skuReport.Count) SKU(s), $($flaggedSkus.Count) above threshold or unhealthy" # ----- Send notification ----- if ($flaggedSkus.Count -eq 0 -and -not $AlwaysSend) { Write-Output "No SKU crossed the $ThresholdPercent% threshold - no email sent (use -AlwaysSend for status mails)" } else { $recipients = @($EmailRecipients -split "," | ForEach-Object { $_.Trim() } | Where-Object { $_ }) $subject = if ($flaggedSkus.Count -gt 0) { "Intune Alert: $($flaggedSkus.Count) license SKU(s) above $ThresholdPercent% utilization" } else { "Intune Status: license utilization healthy" } $body = New-EmailBody -SkuReport @($skuReport) -Threshold $ThresholdPercent $sendResult = Send-EmailNotification -Recipients $recipients -Subject $subject -Body $body if (-not $sendResult) { throw "Email notification failed" } } # Summary Write-Output "`n========================================" Write-Output "License Threshold Alert Summary" Write-Output "========================================" foreach ($row in ($skuReport | Sort-Object UtilizationPct -Descending)) { $flagLabel = if ($row.Flagged) { " [FLAGGED]" } else { "" } Write-Output "$($row.SkuPartNumber): $($row.Consumed)/$($row.Enabled) ($($row.UtilizationPct)%)$flagLabel" } Write-Output "========================================" } catch { Write-Error "Script execution failed: $($_.Exception.Message)" exit 1 } finally { try { $null = Disconnect-MgGraph Write-Output "✓ Disconnected from Microsoft Graph" } catch { Write-Verbose "Graph disconnection completed" } } # Copia sin cambios de https://github.com/ugurkocde/IntuneAutomation (commit 797bb2e). # MIT License # # Copyright (c) 2025 Ugur Koc | Microsoft MVP # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE.