Azure PIM Audit report on role activation using powershell

MrFlinstone 711 Reputation points
2025-11-05T08:47:42.9633333+00:00

I have come across a PowerShell script that reports on Azure role activations, the script works quite well and somewhat provides the required information, however it is missing some role activations and i am not sure what need to be changed on the script to ensure that it captures all role activations.

I have noticed that the script doesn't get the full role assignment for all users, which could explain the reason why some of the activations are being missed. Is there a way to improve the script to get all the role assignments and the last activation date for a user for each assigned role.

The script can be found below.

$useWorkspace 
$workspaceId 
$exportFilePath 
$historyToQueryInDays 
 
# Get users that are PIM eligible
Connect-MgGraph 
 
Write-Host 
 
if
    
    
    
 
    
    
 
    
    
        
        
            
            
            
        
"@
 
    
} 
    
    
 
    
    
    
        
        
 
        
            
            
            
            
            
            
        
    
}
 
# Get all eligible role assignments
$eligibleRoles 
 
 
# Get accounts to reference
$users 
$servicePrincipals 
$groups 
$processedEligibleRoles 
 
foreach 
    
    
    
    
        
        
    
    
        
        
    
     
    
    
        
        
 
        
            
            
            
            
            
            
            
            
            
            
            
            
            
        
        
    
    
        
            
 
            
            
             
            
                
                
                
                
                
                
                
                
                
                
                
                
                
            
        
        
    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    
}
 
# Export the result as a CSV then open it
$processedEligibleRoles 
Invoke-Item 
Microsoft Security | Microsoft Entra | Other
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.