Export Microsoft 365 User Assigned License Report

Export Microsoft 365 User Assigned License Report
This commit is contained in:
AdminDroid 2023-07-17 16:00:56 +05:30
parent 31d591c3fb
commit 235edb60b8

View File

@ -2,6 +2,19 @@
============================================================================================= =============================================================================================
Name: Export Office 365 users' license report Name: Export Office 365 users' license report
website: o365reports.com website: o365reports.com
~~~~~~~~~~~~~~~~~
Script Highlights:
~~~~~~~~~~~~~~~~~
1. The script uses MS Graph PowerShell and installs MS Graph PowerShell SDK (if not installed already) upon your confirmation.
2. It can be executed with certificate-based authentication (CBA) too.
3. Exports Office 365 user license report to CSV file.
4. You can choose to either export license report for all office 365 users or pass an input file to get license report of specific users alone.
5. License Name is shown with its friendly name like Office 365 Enterprise E3 rather than ENTERPRISEPACK.
6. The script can be executed with MFA enabled account too.
7. The script gives 2 output files. One with the detailed report of O365 Licensed users another with the simple details.
For detailed Script execution: https://o365reports.com/2018/12/14/export-office-365-user-license-report-powershell/ For detailed Script execution: https://o365reports.com/2018/12/14/export-office-365-user-license-report-powershell/
============================================================================================ ============================================================================================
#> #>
@ -16,20 +29,20 @@ Param
) )
Function ConnectMgGraphModule Function ConnectMgGraphModule
{ {
$MsGraphModule = Get-Module Microsoft.Graph -ListAvailable $MsGraphBetaModule = Get-Module Microsoft.Graph.Beta -ListAvailable
if($MsGraphModule -eq $null) if($MsGraphBetaModule -eq $null)
{ {
Write-host "Important: Microsoft Graph PowerShell module is unavailable. It is mandatory to have this module installed in the system to run the script successfully." Write-host "Important: Microsoft Graph Beta module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
$confirm = Read-Host Are you sure you want to install Microsoft graph module? [Y] Yes [N] No $confirm = Read-Host Are you sure you want to install Microsoft Graph Beta module? [Y] Yes [N] No
if($confirm -match "[yY]") if($confirm -match "[yY]")
{ {
Write-host "Installing Microsoft Graph PowerShell module..." Write-host "Installing Microsoft Graph Beta module..."
Install-Module Microsoft.Graph -Scope CurrentUser Install-Module Microsoft.Graph.Beta -Scope CurrentUser -AllowClobber
Write-host "Microsoft Graph PowerShell module is installed in the machine successfully" -ForegroundColor Magenta Write-host "Microsoft Graph Beta module is installed in the machine successfully" -ForegroundColor Magenta
} }
else else
{ {
Write-host "Exiting. `nNote: Microsoft Graph PowerShell module must be available in your system to run the script" -ForegroundColor Red Write-host "Exiting. `nNote: Microsoft Graph Beta module must be available in your system to run the script" -ForegroundColor Red
Exit Exit
} }
} }
@ -51,7 +64,7 @@ Function ConnectMgGraphModule
Exit Exit
} }
} }
Write-Host "Microsoft Graph PowerShell module is connected successfully" -ForegroundColor Green Write-Host "Microsoft Graph Beta PowerShell module is connected successfully" -ForegroundColor Green
} }
Function Get_UsersLicenseInfo Function Get_UsersLicenseInfo
@ -65,19 +78,18 @@ Function Get_UsersLicenseInfo
$Country="-" $Country="-"
} }
Write-Progress -Activity "`n Exported user count:$LicensedUserCount "`n"Currently Processing:$upn" Write-Progress -Activity "`n Exported user count:$LicensedUserCount "`n"Currently Processing:$upn"
$SKUs = Get-MgUserLicenseDetail -UserId $UPN $SKUs = Get-MgBetaUserLicenseDetail -UserId $UPN -ErrorAction SilentlyContinue
$LicenseCount = $SKUs.count $LicenseCount = $SKUs.count
$count = 0 $count = 0
foreach($Sku in $SKUs) #License loop foreach($Sku in $SKUs) #License loop
{ {
$EasyName = $FriendlyNameHash[$Sku.SkuPartNumber] if($FriendlyNameHash[$Sku.SkuPartNumber])
if(!($EasyName))
{ {
$NamePrint = $Sku.SkuPartNumber $NamePrint = $FriendlyNameHash[$Sku.SkuPartNumber]
} }
else else
{ {
$NamePrint = $EasyName $NamePrint = $Sku.SkuPartNumber
} }
#Get all services for current SKUId #Get all services for current SKUId
$Services = $Sku.ServicePlans $Services = $Sku.ServicePlans
@ -109,8 +121,15 @@ Function Get_UsersLicenseInfo
$flag = 1 $flag = 1
} }
#Convert ServiceName to friendly name #Convert ServiceName to friendly name
$ServiceFriendlyName = $ServiceArray|?{$_.Service_Plan_Name -eq $ServiceName} $ServiceFriendlyName = $ServiceArray|Where-Object{$_.Service_Plan_Name -eq $ServiceName}
$ServiceFriendlyName = $ServiceFriendlyName[0].ServiceFriendlyNames if($ServiceFriendlyName -ne $Null)
{
$ServiceFriendlyName = $ServiceFriendlyName[0].ServiceFriendlyNames
}
else
{
$ServiceFriendlyName = $ServiceName
}
if($flag -eq 1) if($flag -eq 1)
{ {
if($EnabledServiceCount -ne 1) if($EnabledServiceCount -ne 1)
@ -143,14 +162,19 @@ function CloseConnection
Function main() Function main()
{ {
ConnectMgGraphModule ConnectMgGraphModule
Select-MgProfile beta Write-Host "`nNote: If you encounter module related conflicts, run the script in a fresh PowerShell window." -ForegroundColor Yellow
#Set output file #Set output file
$ExportCSV = ".\DetailedO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" $ExportCSV = ".\DetailedO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
$ExportSimpleCSV = ".\SimpleO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" $ExportSimpleCSV = ".\SimpleO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
#FriendlyName list for license plan and service #FriendlyName list for license plan and service
try{ try{
$FriendlyNameHash = Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction SilentlyContinue | ConvertFrom-StringData $FriendlyNameHash = Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction SilentlyContinue -ErrorVariable ERR | ConvertFrom-StringData
$ServiceArray = Import-Csv -Path .\ServiceFriendlyName.csv -ErrorAction SilentlyContinue if($ERR -ne $null)
{
Write-Host $ERR -ForegroundColor Red
CloseConnection
}
$ServiceArray = Import-Csv -Path .\ServiceFriendlyName.csv
} }
catch catch
{ {
@ -167,24 +191,24 @@ Function main()
$UserNames = Import-Csv -Header "UserPrincipalName" $UserNamesFile $UserNames = Import-Csv -Header "UserPrincipalName" $UserNamesFile
foreach($item in $UserNames) foreach($item in $UserNames)
{ {
Get-MgUser -UserId $item.UserPrincipalName |where{$_.AssignedLicenses -ne $null} | Foreach{ Get-MgBetaUser -UserId $item.UserPrincipalName -ErrorAction SilentlyContinue |Where-Object{$_.AssignedLicenses -ne $null} | ForEach-Object{
Get_UsersLicenseInfo Get_UsersLicenseInfo
$LicensedUserCount++ $LicensedUserCount++
} }
} }
} }
#Get all licensed users #Get all licensed users
else else
{ {
Get-MgUser -All | where{$_.AssignedLicenses -ne $null} | Foreach{ Get-MgBetaUser -All |Where-Object{$_.AssignedLicenses -ne $null} | ForEach-Object{
Get_UsersLicenseInfo Get_UsersLicenseInfo
$LicensedUserCount++} $LicensedUserCount++
}
} }
#Open output file after execution #Open output file after execution
if((Test-Path -Path $ExportCSV) -eq "True") if((Test-Path -Path $ExportCSV) -eq "True")
{ { Write-Host `n "Detailed report available in:" -NoNewline -ForegroundColor Yellow; Write-Host "$ExportCSV"
Write-Host Detailed report available in: $ExportCSV Write-Host `n "Simple report available in:" -NoNewline -ForegroundColor Yellow; Write-Host "$ExportSimpleCSV" `n
Write-host Simple report available in: $ExportSimpleCSV
$Prompt = New-Object -ComObject wscript.shell $Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open output files?",` 0,"Open Files",4) $UserInput = $Prompt.popup("Do you want to open output files?",` 0,"Open Files",4)
if($UserInput -eq 6) if($UserInput -eq 6)
@ -195,8 +219,10 @@ Function main()
} }
else else
{ {
Write-Host "No data found" -ForegroundColor Red Write-Host "No data found"
} }
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
CloseConnection CloseConnection
} }
. main . main