add: Error handling for Get-AdminRoleUserLicense
This commit is contained in:
@@ -13,37 +13,41 @@ function Get-AdminRoleUserLicense {
|
|||||||
$adminRoleUsers = @()
|
$adminRoleUsers = @()
|
||||||
$userIds = @()
|
$userIds = @()
|
||||||
}
|
}
|
||||||
Process { # Connect to Microsoft Graph if not skipping connection
|
|
||||||
|
|
||||||
|
Process {
|
||||||
$adminroles = Get-MgRoleManagementDirectoryRoleDefinition | Where-Object { $_.DisplayName -like "*Admin*" }
|
$adminroles = Get-MgRoleManagementDirectoryRoleDefinition | Where-Object { $_.DisplayName -like "*Admin*" }
|
||||||
|
|
||||||
foreach ($role in $adminroles) {
|
foreach ($role in $adminroles) {
|
||||||
$usersInRole = Get-MgRoleManagementDirectoryRoleAssignment -Filter "roleDefinitionId eq '$($role.Id)'"
|
$usersInRole = Get-MgRoleManagementDirectoryRoleAssignment -Filter "roleDefinitionId eq '$($role.Id)'"
|
||||||
|
|
||||||
foreach ($user in $usersInRole) {
|
foreach ($user in $usersInRole) {
|
||||||
$userIds += $user.PrincipalId
|
$userDetails = Get-MgUser -UserId $user.PrincipalId -Property "DisplayName, UserPrincipalName, Id, onPremisesSyncEnabled" -ErrorAction SilentlyContinue
|
||||||
$userDetails = Get-MgUser -UserId $user.PrincipalId -Property "DisplayName, UserPrincipalName, Id, onPremisesSyncEnabled"
|
|
||||||
|
|
||||||
$adminRoleUsers += [PSCustomObject]@{
|
if ($userDetails) {
|
||||||
RoleName = $role.DisplayName
|
$userIds += $user.PrincipalId
|
||||||
UserName = $userDetails.DisplayName
|
$adminRoleUsers += [PSCustomObject]@{
|
||||||
UserPrincipalName = $userDetails.UserPrincipalName
|
RoleName = $role.DisplayName
|
||||||
UserId = $userDetails.Id
|
UserName = $userDetails.DisplayName
|
||||||
HybridUser = $userDetails.onPremisesSyncEnabled
|
UserPrincipalName = $userDetails.UserPrincipalName
|
||||||
Licenses = "" # Placeholder for licenses, to be filled later
|
UserId = $userDetails.Id
|
||||||
|
HybridUser = $userDetails.onPremisesSyncEnabled
|
||||||
|
Licenses = $null # Initialize as $null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($userId in $userIds | Select-Object -Unique) {
|
foreach ($userId in $userIds | Select-Object -Unique) {
|
||||||
$licenses = Get-MgUserLicenseDetail -UserId $userId
|
$licenses = Get-MgUserLicenseDetail -UserId $userId -ErrorAction SilentlyContinue
|
||||||
$licenseList = ($licenses.SkuPartNumber -join '|')
|
if ($licenses) {
|
||||||
|
$licenseList = ($licenses.SkuPartNumber -join '|')
|
||||||
$adminRoleUsers | Where-Object { $_.UserId -eq $userId } | ForEach-Object {
|
$adminRoleUsers | Where-Object { $_.UserId -eq $userId } | ForEach-Object {
|
||||||
$_.Licenses = $licenseList
|
$_.Licenses = $licenseList
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
End {
|
End {
|
||||||
Write-Host "Disconnecting from Microsoft Graph..." -ForegroundColor Green
|
Write-Host "Disconnecting from Microsoft Graph..." -ForegroundColor Green
|
||||||
Disconnect-MgGraph | Out-Null
|
Disconnect-MgGraph | Out-Null
|
||||||
|
Reference in New Issue
Block a user