I have the below PowerShell script where I want to make the following changes:
- Dedupe the list of users as some users might have more than one license SKU. My current method adds a user more than once if they have more than one license SKU assigned. Each user should only be included in my list once.
- Add a column for each license SKU to indicate true/false on whether the user has that specific license SKU. i.e. There should be a true/false column for each SKU (and the number of SKUs could change over time).
Any assistance would be greatly appreciated!
Thanks!
Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All" -NoWelcome
Add-Type -AssemblyName System.Web
$uri = [System.UriBuilder] 'https://graph.microsoft.com'
function Get-GroupMembersId {
[CmdletBinding()]
param([string] $uri)
do {
$response = Invoke-MgGraphRequest GET $uri -Headers @{ ConsistencyLevel = 'eventual' }
$uri = $response.'@odata.nextLink'
if ($null -ne $response.value) {
$response.value.Id
}
}
while ($uri)
}
$tmSku = Get-MgSubscribedSku -All | Where SkuPartNumber -in ('SKU1', 'SKU2', 'SKU3', 'SKU4', 'SKU5')
foreach ($sku in $tmSku)
{
$getMgUserSplat = @{
Filter = "assignedLicenses/any(x: x/skuId eq $($sku.SkuId))"
Select = 'DisplayName', 'Id', 'Mail', 'UserPrincipalName', 'JobTitle'
All = $true
}
$users += Get-MgUser @getMgUserSplat
$userCount = $users | Measure-Object
}
$result = foreach ($user in $users) {
$outObject = [ordered]@{
Id = $user.Id
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
Mail = $user.Mail
JobTitle = $user.JobTitle
}
[pscustomobject] $outObject
}
Write-Host "Found " $userCount.Count " licensed users."
$result