I need to know which Privileged Identity Management groups a specified user in Entra ID…
- User
I need to know which Privileged Identity Management groups a specified user in Entra ID is Eligible to. Azure tenant name = akademikerpension user name = kkjmppension.onmicrosoft.com How can i do that using powershell core?
- Assistant
To determine which Privileged Identity Management (PIM) groups a specific user in Entra ID (formerly Azure Active Directory) is eligible for, you can leverage the Microsoft Graph PowerShell module. Below are detailed steps and sample scripts to help you accomplish this using PowerShell Core.
Prerequisites
-
Permissions: Ensure you have the necessary permissions to access PIM data. Typically, you need roles like Privileged Role Administrator or Global Administrator in your Azure AD tenant.
-
PowerShell Core: Ensure you are using PowerShell Core (version 7.x or later) for compatibility with the Microsoft Graph module.
Step-by-Step Guide
1. Install the Microsoft Graph PowerShell Module
If you haven't installed the Microsoft Graph module yet, run the following command:
Install-Module Microsoft.Graph -Scope CurrentUserNote: If prompted about installing from an untrusted repository, type
Yand press Enter to confirm.2. Import the Microsoft Graph Module
Import the module into your PowerShell session:
Import-Module Microsoft.Graph3. Connect to Microsoft Graph
Establish a connection to Microsoft Graph with the required permissions. You'll need at least the
PrivilegedAccess.Read.Allpermission to read PIM assignments.Connect-MgGraph -Scopes "PrivilegedAccess.Read.All"A browser window will open prompting you to sign in. Use an account that has the necessary administrative permissions in your Azure AD tenant.
4. Retrieve the User Object
Fetch the user object based on the User Principal Name (UPN):
# Define the user UPN $userUpn = "kkjmppension.onmicrosoft.com" # Retrieve the user object $user = Get-MgUser -UserId $userUpn # Extract the User ID $userId = $user.Id5. Retrieve Eligible PIM Group Assignments
PIM group assignments can be accessed via the Privileged Access feature in Microsoft Graph. Here's how to retrieve the eligible group assignments for the specified user:
# Define the Azure AD tenant ID or use default if already connected $tenantId = "akademikerpension.onmicrosoft.com" # Construct the API endpoint for eligible group assignments # Note: Replace 'v1.0' with 'beta' if necessary, as some PIM features may currently be in beta. $apiUrl = "https://graph.microsoft.com/v1.0/privilegedAccess/group/eligibilityScheduleRequests?$filter=principal/id eq '$userId'" # Invoke the API request $response = Invoke-MgGraphRequest -Method GET -Uri $apiUrl # Check if any eligible assignments are returned if ($response.value.Count -eq 0) { Write-Output "No eligible PIM group assignments found for user $userUpn." } else { Write-Output "Eligible PIM Groups for user $userUpn:`n" # Iterate through each eligible assignment and retrieve group details foreach ($assignment in $response.value) { # Get the Group ID from the assignment $groupId = $assignment.resourceId # Retrieve group details $group = Get-MgGroup -GroupId $groupId # Output the group display name Write-Output "- $($group.DisplayName)" } }Explanation of the Script:
-
API Endpoint Construction:
- The script constructs a GET request to the
privilegedAccess/group/eligibilityScheduleRequestsendpoint, filtering assignments where theprincipal/idmatches the user's ID. This retrieves all eligible group assignments for the specified user.
- The script constructs a GET request to the
-
Invoke-MgGraphRequest:
- This cmdlet sends the constructed HTTP request to Microsoft Graph and retrieves the response.
-
Processing the Response:
- If eligible assignments are found, the script iterates through each assignment, retrieves the corresponding group details using the
Get-MgGroupcmdlet, and outputs the group’s display name.
- If eligible assignments are found, the script iterates through each assignment, retrieves the corresponding group details using the
6. Sample Output
After running the script, you might see output similar to the following if the user has eligible PIM group assignments:
Eligible PIM Groups for user kkjmppension.onmicrosoft.com: - Finance Administrators - HR Managers - IT Support TeamIf the user has no eligible assignments, the script will output:
No eligible PIM group assignments found for user kkjmppension.onmicrosoft.com.Additional Considerations
-
API Versions: Some PIM features in Microsoft Graph might still be in the
betaversion. Always check the Microsoft Graph documentation for the latest information and adjust the API endpoints accordingly. -
Error Handling: For production scripts, consider adding error handling to manage scenarios where the API call fails or returns unexpected results.
-
Automating Permissions: Ensure that the account running the script has been granted the necessary API permissions in Azure AD and that admin consent has been provided.
-
Module Updates: Microsoft frequently updates the Graph PowerShell modules. Keep your modules up to date to access the latest features and cmdlets.
References
- Microsoft Graph PowerShell SDK Documentation
- Privileged Access API in Microsoft Graph
- Azure AD PIM Documentation
By following the steps above, you should be able to identify which PIM groups the specified user is eligible for within your Entra ID tenant using PowerShell Core.
-