Forum Discussion
SauPom
Aug 15, 2023Copper Contributor
Get-MsolUser primary SMTP and other info
I need to get primary SMTP address and also other info like country etc. I run this command, but I don´t know how to add other info after Select. Get-MsolUser -All | Select -Expand Proxyaddre...
SauPom
Copper Contributor
Thank you for your reply. Actually that last works really well other than I also need to add to script licenses property and I cannot find that from Get-MgBetaUser. Any advice on that?
It´s like I need to get all the users with E3 license sorted by country and primary SMTP.
It´s like I need to get all the users with E3 license sorted by country and primary SMTP.
LainRobertson
Aug 17, 2023Silver Contributor
It can be done but it's not "easy" - particularly if you want the resulting data to mean something to the people reading the final report.
Here's an example script that illustrates one approach.
Note: A user can - and almost always will - have more than one product licenced to them, hence productNames is an array.
Example
# Import the Microsoft CSV containing the translations. I'm going to use a Dictionary for storage as it's faster on lookups.
$MsProducts = [System.Collections.Generic.Dictionary[[guid],[string]]]::new();
Import-Csv -Path "D:\Data\ms-products-and-plans.csv" |
ForEach-Object {
if (-not $MsProducts.ContainsKey($_.GUID))
{
$MsProducts.Add($_.GUID, $_.Product_Display_Name);
}
}
# Pull the list of users from Graph and output just the data we're interested in.
Get-MgBetaUser -All -Property * |
ForEach-Object {
[PSCustomObject] @{
id = $_.id;
userPrincipalName = $_.userPrincipalName;
country = $_.country;
mail = $_.mail;
productNames = $_.AssignedLicenses |
ForEach-Object {
if ($null -ne ($Name = $MsProducts[$_.SkuId]))
{
$Name;
}
else
{
"Unlisted product ($($_.SkuId))";
}
}
}
}
Output
Cheers,
Lain
- SauPomAug 21, 2023Copper ContributorThanks for the reply.
I actually don´t need to know, what licenses user has. I only I need list of all users, that has E3 license assigned.
For me list would look like:
Mail, country
I only need mail and country info for all the users that has E3 assigned.- LainRobertsonAug 21, 2023Silver Contributor
That doesn't change the script all that much.
The efficient thing to do is apply the "E3" filter on the $MsProducts list (using Excel, there was circa 270 products featuring "E3"), after which it's mostly the same process for comparing the users to the product list.
Example
<# To derive something meaningful from the dog's breakfest that is the Graph licencing commandlets, we have to: 1. Import the CSV Microsoft provides containing the GUID-to-product mappings, to be used as a lookup table. The CSV URL can be found in the following doco: https://learn.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-service-plan-reference 2. Get your users via Graph and translating the SkuId GUID into something human-readable by lookup up this GUID from the CSV data above. #> # Import the Microsoft CSV containing the translations. I'm going to use a Dictionary for storage as it's faster on lookups. $MsProducts = [System.Collections.Generic.Dictionary[[guid],[string]]]::new(); Import-Csv -Path "D:\Data\ms-products-and-plans.csv" | ForEach-Object { if ((-not $MsProducts.ContainsKey($_.GUID)) -and ($_.Product_Display_Name.Contains("E3") -or $_.Service_Plan_Id.Contains("E3"))) { $MsProducts.Add($_.GUID, $_.Product_Display_Name); } } # Pull the list of users from Graph and output just the data we're interested in. Get-MgBetaUser -All -Property mail, country, assignedLicenses | ForEach-Object { foreach ($Licence in $_.AssignedLicenses) { if ($MsProducts.ContainsKey($Licence.SkuId)) { [PSCustomObject] @{ mail = $_.mail; country = $_.country; } break; } } }
Output
Cheers,
Lain