Forum Discussion

mfranhind115's avatar
mfranhind115
Brass Contributor
Jul 14, 2022
Solved

Powershell: get data from MsOnline

Hi all,

I would extract some "basic" data like "UserPrincipalName, DisplayName, licenses, islicensed, creation date, last activation date" and others "nested" like AlternateEmailAddresses.

 

If possible, but only if possible without struggling, also the expiry date of the licenses...

 

More, I would filter only some kind of licenses, not everything but only:

 

  • #EXCHANGESTANDARD
  • #EXCHANGEENTERPRISE
  • #O365_BUSINESS
  • #O365_BUSINESS_ESSENTIALS
  • #O365_BUSINESS_PREMIUM
  • #SPB
  • #ATP_ENTERPRISE
  • #POWER_BI_PRO

 

I tried several attempts without any success...

 

A further step could be (but maybe not possible, perhaps difficult I understand) to get the licenses in separate columns, one for each type, to have a better ovwerview of the status of the accounts...

 

May someone help?

 

thanks a lot for your support

mf

  • mfranhind115 Wrote a post about getting the license state and details for all users a while ago, perhaps a good start to expand on?

     

    #Connect to MSOL if not connected
    Write-Host Checking MSOnline module -ForegroundColor Green
    try {
        Get-MsolDomain -ErrorAction Stop | Out-Null
    }
    catch {
        if (-not (get-module -ListAvailable | Where-Object Name -Match 'MSOnline')) {
            Write-Host Installing MSOnline module.. -ForegroundColor Green
            Install-Module MSOnline
        }
        Connect-MsolService
    }
     
    #Create table of users and licenses (https://docs.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-service-plan-reference)
    #Download csv with all SKU's
    $ProgressPreference = "SilentlyContinue"
    Write-Host Downloading license overview from Microsoft -ForegroundColor Green
    $csvlink = ((Invoke-WebRequest -Uri https://docs.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-service-plan-reference).Links | where-Object Href -Match 'CSV').href
    Invoke-WebRequest -Uri $csvlink -OutFile $env:TEMP\licensing.csv
    $skucsv = Import-Csv -Path $env:TEMP\licensing.csv
    $UsersLicenses = @()
    foreach ($user in Get-MsolUser -All | Sort-Object UserPrincipalName) {
        if ($user.isLicensed -eq $True) {
            foreach ($License in $User.licenses) {
                $SKUfriendlyname = $skucsv | Where-Object String_Id -Contains $License.AccountSkuId.Split(':')[1] | Select-Object Product_Display_Name -First 1
                $SKUserviceplan = $skucsv | Where-Object String_Id -Contains $License.AccountSkuId.Split(':')[1] | Sort-Object Service_Plans_Included_Friendly_Names
                foreach ($serviceplan in $SKUserviceplan) {
                    $Licenses = [PSCustomObject]@{
                        User        = $User.UserPrincipalName
                        LicenseSKU  = $SKUfriendlyname.Product_Display_Name
                        Serviceplan = $serviceplan.Service_Plans_Included_Friendly_Names
                    }
                    $UsersLicenses += $Licenses
                }
            }
        }   
    }
     
    #Output all license information to c:\temp\userslicenses.csv and open it
    $UsersLicenses | Sort-Object User, LicenseSKU, Serviceplan | Export-Csv -NoTypeInformation -Delimiter ';' -Encoding UTF8 -Path c:\temp\userslicenses.csv
    Invoke-Item c:\temp\userslicenses.csv

    (https://powershellisfun.com/2022/05/16/microsoft-365-license-overview-per-user/)

    But could you show us what you already have and how the output of that is?

    • mfranhind115's avatar
      mfranhind115
      Brass Contributor

      Hi Harm and many thanks

      I would try your suggestion soon and let you know

      at the moment I only extract separately the infos and combine them with excel formulas

      but, for examples, I get all the licenses and features, so I have to check visually what licenses I'm interested of

      then it is a lot time expensive

      • mfranhind115's avatar
        mfranhind115
        Brass Contributor
        just tried the script
        but it seems going stuck requesting for the web
        any chance to avoid that request in your opinion?

Resources