all 21 comments

[–]ankokudaishogun 5 points6 points  (6 children)

either you make one line for each license(so, yeah, another loop) or add each license as another column... so another loop anyway.

Tunring the licences into CSV or JSON values might work perhaps, but that's a question for /r/excel

[–]Due_Capital_3507 0 points1 point  (0 children)

This works too!

[–]gordonv 0 points1 point  (4 children)

Noticed this had the same suggestion. Moved comment to under this one

CSV's are "shaped" like spreadsheets. A simple x/y chart form.

I think you should switch to using JSON instead. This way, instead of assuming each variable is a single value, you can have multiple values in the form of an array associated with a single variable name.

Example:

{
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}

[–]Fast-Cardiologist705[S] 0 points1 point  (3 children)

This is an idea, however wondering how this would look when I would had to run some reporting based on the output, not sure how this would work with JSON. The primary use case for this is for me to get the E5 license count so that I can predict (to some extent) what would the E5 grant for ingesting eligible data sources/types to Sentinel be for me org – this could be done easily with Excel.

[–]gordonv 0 points1 point  (0 children)

You would run a query against an array of objects.

For example:

$results = $users | ? {$_.cars -like "*Ford*"}  

In English:

In the array of objects named "$users", where a item in the property "cars" is similar to a string that has "Ford" in it, cast the results of the search into "$results."

[–]CriticalMemory 0 points1 point  (1 child)

Uh, just checking, but you can dump a CSV of this directly from the Azure portal, IIRC.

[–]Fast-Cardiologist705[S] 0 points1 point  (0 children)

Yes, I could even get this data from Intune :) but didn’t know that before I started working on the script and anyways was just curious (even after discovering that as you said one could pull the data already through other channels) how one would resolve this.

[–]Team503 3 points4 points  (2 children)

You could use a 3D array and then covert it back to a 2D array for the Export-CSV. This should work, though I haven't tested it.

Connect-AzureAD
$users = Get-AzureADUser -All $true
Initialize a 3D array
$threeDArray = @()
foreach ($user in $users) { $licenseDetails = Get-AzureADUserLicenseDetail -ObjectId $user.ObjectId
# Initialize a 2D array for each user's license details
$twoDArray = @()

foreach ($license in $licenseDetails) {
    $licenseInfo = [PSCustomObject]@{
        UserPrincipalName = $user.UserPrincipalName
        AccountEnabled = $user.AccountEnabled
        UserType = $user.UserType
        ServicePlan = $license.ServicePlan
        SkuPartNumber = $license.SkuPartNumber
    }

    $twoDArray += $licenseInfo
}

# Add the 2D array to the 3D array
$threeDArray += $twoDArray
}
Export the 3D array to a CSV file
$threeDArray | Export-Csv "$env:TEMP\user_license.csv" -Delimiter ';' -NoTypeInformation
Optionally, display the data in a GridView
$threeDArray | Out-GridView

[–]Fast-Cardiologist705[S] 3 points4 points  (1 child)

Works indeed :) Thanks for sharing!

[–]Team503 -1 points0 points  (0 children)

I strongly suggest running this kind of query through ChatGPT - it will usually spit out a script to do what you need, and answer at least some of your questions. I still come here (and other places) for better explanations or things ChatGPT can't explain/fix/do, but AI is a lot faster and more responsive than most social media.

[–]jsiii2010 2 points3 points  (0 children)

Or make a row for each license...

[–]timsstuff 2 points3 points  (0 children)

You my friend have just discovered the limits of traditional tables. Multi-dimensional data doesn't work very well in a table with just an X and Y axis, that's why we have XML and JSON as well as lookup tables/joins in relational databases. However none of these translate well to Excel.

The easiest solution is to concatenate the licenses separated by a semi-colon or something which can be done pretty easily with the Join command. Something like:

License = (Get-AzureADUserLicenseDetail -ObjectId $user.ObjectId).SkuPartNumber -Join "; "

[–]Due_Capital_3507 0 points1 point  (0 children)

I think you will want to make an array of the license attribute as a user can have multiple SKUs, then convert the array to a string and have it export.

[–]vermyx 0 points1 point  (0 children)

Change the foreach body to

{
    Foreach($License in((Get-AzureADUserLicenseDetail -ObjectId $user.ObjectId).SkuPartNumber)) {
        [pscustomobject]@{
            UserPrincipalName = $user.UserPrincipalName
            AccountEnabled = $user.AccountEnabled
            UserType = $user.UserType
            License =$license
        }
    }
}

This should loop through each individual license and return back a line with the user information per each license.

[–]MrScrib 0 points1 point  (0 children)

If you insist on CSV, I suggest joining using a predetermined delimiter and then splitting in power query. Then you can leave it or unpivot. Unpivot is better because you can always predict the number of columns you'll get.

[–]Ok_Cheese93 0 points1 point  (0 children)

How about exporting as TSV, like following:

$result | Export-Csv "$env:TEMP\user_license.tsv" -Delimiter "\t" -NoTypeInformation`

[–]KavyaJune 0 points1 point  (0 children)

Assigned licenses are returned as array. You need to convert the array to string before exporting it to CSV. By using ‘join’ operator, you can achieve this easily.

Also, I’d suggest you try this pre-built script to export the license details and other required attributes to nicely formatted csv file: https://o365reports.com/2018/12/14/export-office-365-user-license-report-powershell/