Skip to content

Instantly share code, notes, and snippets.

@gioxx
Created June 12, 2023 09:46
Show Gist options
  • Save gioxx/5da885c5640c5b7d17d1d352317e5cfb to your computer and use it in GitHub Desktop.
Save gioxx/5da885c5640c5b7d17d1d352317e5cfb to your computer and use it in GitHub Desktop.
La funzione che utilizzo per esportare le licenze in uso nel tenant Microsoft 365. Maggiori informazioni disponibili qui: https://wp.me/pdQ5q-tKh - Dai un'occhiata al modulo completo "ToyBox" disponibile all'indirizzo https://github.com/gioxx/ps.toybox
function MsolAccountSku-Export {
param(
[Parameter(Mandatory=$false, ValueFromPipeline, HelpMessage="Folder where export CSV file (e.g. C:\Temp)")][string] $folderCSV
)
if ( (Get-Module -Name Microsoft.Graph -ListAvailable).count -eq 0 ) {
Write-Host "Please install the Graph module using this command (then relaunch this script): `nInstall-Module Microsoft.Graph" -f "Yellow"
exit
} else { Connect-MgGraph | Out-Null }
if ( (Get-Module -Name Microsoft.Graph.Users -ListAvailable).count -eq 0 ) {
Write-Host "Please install the Microsoft.Graph.Users module using this command (then relaunch this script): `nInstall-Module Microsoft.Graph.Users" -f "Yellow"
exit
} else {
if ( (Get-Module -Name Microsoft.Graph.Users).count -eq 0 ) {
Import-Module Microsoft.Graph.Users
}
}
Set-Variable ProgressPreference Continue
if ([string]::IsNullOrEmpty($folderCSV)) {
$folderCSV = "C:\Temp"
} else {
$folderCSV = $folderCSV.TrimEnd('\')
}
$Today = Get-Date -format yyyyMMdd
$Result=@()
$ProcessedCount = 0
$licenseFile = Invoke-RestMethod -Method Get -Uri 'https://raw.githubusercontent.com/gioxx/ps.toybox/main/JSON/M365_licenses.json'
$Users = Get-MgUser -Filter 'assignedLicenses/$count ne 0' -ConsistencyLevel eventual -CountVariable totalUsers -All
$Users | Foreach-Object {
$ProcessedCount++
$PercentComplete = (($ProcessedCount / $totalUsers) * 100)
$User = $_
Write-Progress -Activity "Processing $($User.DisplayName)" -Status "$ProcessedCount out of $totalUsers ($($PercentComplete.ToString('0.00'))%)" -PercentComplete $PercentComplete
$GraphLicense = Get-MgUserLicenseDetail -UserId $User.Id
if ($GraphLicense -ne $null) {
ForEach ( $License in $($GraphLicense.SkuPartNumber) ) {
ForEach ( $LicenseStringId in $licenseFile ) {
if ( $LicenseStringId.String_Id -eq $License ) {
$Result += New-Object -TypeName PSObject -Property $([ordered]@{
DisplayName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
PrimarySmtpAddress = $User.Mail
Licenses = $LicenseStringId.Product_Display_Name
})
break
}
}
}
}
}
$CSV = SaveFileWithProgressiveNumber("$($folderCSV)\O365-User-License-Report_$($Today).csv")
$Result | Export-CSV $CSV -NoTypeInformation -Encoding UTF8 -Delimiter ";"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment