I used this script from the repository
https://www.adaxes.com/script-repository/check-if-number-of-unused-microsoft-365-licenses-is-below-limit-s594.htm
I have amended to include the skus I am concerned about so it now shows this:
$limit = 2 # TODO: modify me
$skus = @("ENTERPRISEPACK","EMS") # TODO: modify me
$Context.ConditionIsMet = $False
$tenantDN = "%adm-AssociatedO365Tenant%"
if ([System.String]::IsNullOrEmpty($tenantDN))
{
$Context.LogMessage("No Microsoft 365 tenant is associated with the user", "Error")
return
}
# Bind to Microsoft 365 tenant
$tenant = $Context.BindToObjectByDN($tenantDN)
# Check licenses
foreach ($sku in $tenant.Skus)
{
if (($skus -ne $NULL) -and ($skus -notcontains $sku.SkuPartNumber))
{
continue
}
$difference = $sku.TotalUnits - $sku.ConsumedUnits
if ($difference -gt $limit)
{
continue
}
$Context.ConditionIsMet = $True
}
I am using this in the 'Before user creation' area:
I just want to receive an email when the license count is below what I specify.
Please can you advise what I am doing wrong.