2021-03-02 19:44:15 +05:30
<#
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2023-04-25 13:12:05 +05:30
Name : Export Microsoft 365 Admin Report using MS Graph PowerShell
2021-03-02 19:44:15 +05:30
Description : This script exports Microsoft 365 admin role group membership to CSV
2023-04-25 13:12:05 +05:30
Version : 3.0
2021-03-02 19:44:15 +05:30
website : o365reports . com
2023-07-17 13:25:39 +05:30
Script Highlights :
The script uses MS Graph PowerShell and installs MS Graph PowerShell SDK-beta ( if not installed already ) upon your confirmation .
It supports MFA-enabled admin accounts too .
It can be executed with certificate-based authentication ( CBA ) too .
With a simple execution format , you can achieve all admins ’ report and role-based admin report .
Helps to find admin roles for a specific user ( s ) .
Helps to get all admins with a specific role ( s ) .
The script is scheduler-friendly .
Exports the result to file in the CSV format and also opens the CSV on confirmation .
2021-03-02 19:44:15 +05:30
For detailed Script execution : https : / / o365reports . com / 2021 / 03 / 02 / Export-Office - 365 -admin -role -report -powershell
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
#>
param (
[ switch ] $RoleBasedAdminReport ,
2023-04-25 13:12:05 +05:30
[ switch ] $ExcludeGroups ,
2021-03-02 19:44:15 +05:30
[ String ] $AdminName = $null ,
2023-04-25 13:12:05 +05:30
[ String ] $RoleName = $null ,
[ string ] $TenantId ,
[ string ] $ClientId ,
[ string ] $CertificateThumbprint
)
2021-03-02 19:44:15 +05:30
#Check for module availability
2023-07-17 13:25:39 +05:30
$MsGraphBetaModule = Get-Module Microsoft . Graph . Beta -ListAvailable
if ( $MsGraphBetaModule -eq $null )
2023-04-25 13:12:05 +05:30
{
2023-07-17 13:25:39 +05:30
Write-host " Important: Microsoft Graph Beta module is unavailable. It is mandatory to have this module installed in the system to run the script successfully. "
$confirm = Read-Host Are you sure you want to install Microsoft Graph Beta module ? [ Y] Yes [N ] No
2023-04-25 13:12:05 +05:30
if ( $confirm -match " [yY] " )
{
2023-07-17 13:25:39 +05:30
Write-host " Installing Microsoft Graph Beta module... "
Install-Module Microsoft . Graph . Beta -Scope CurrentUser -AllowClobber
Write-host " Microsoft Graph Beta module is installed in the machine successfully " -ForegroundColor Magenta
2023-04-25 13:12:05 +05:30
}
else
{
2023-07-17 13:25:39 +05:30
Write-host " Exiting. `n Note: Microsoft Graph Beta module must be available in your system to run the script " -ForegroundColor Red
2023-04-25 13:12:05 +05:30
Exit
}
}
if ( ( $TenantId -ne " " ) -and ( $ClientId -ne " " ) -and ( $CertificateThumbprint -ne " " ) )
{
Connect-MgGraph -TenantId $TenantId -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -ErrorAction SilentlyContinue -ErrorVariable ConnectionError | Out-Null
if ( $ConnectionError -ne $null )
{
Write-Host $ConnectionError -Foregroundcolor Red
Exit
}
2021-03-02 19:44:15 +05:30
}
else
2023-04-25 13:12:05 +05:30
{
Connect-MgGraph -Scopes " Directory.Read.All " -ErrorAction SilentlyContinue -Errorvariable ConnectionError | Out-Null
if ( $ConnectionError -ne $null )
{
Write-Host " $ConnectionError " -Foregroundcolor Red
Exit
}
}
2023-07-17 13:25:39 +05:30
Write-Host " Microsoft Graph Beta Powershell module is connected successfully " -ForegroundColor Green
Write-Host " `n Note: If you encounter module related conflicts, run the script in a fresh Powershell window. " -ForegroundColor Yellow
Write-Host " `n Preparing admin report... "
2023-04-25 13:12:05 +05:30
$Admins = @ ( )
$RoleList = @ ( )
$OutputCsv = " .\AdminReport_ $( ( Get-Date -format MMM-dd ` hh-mm ` tt ) . ToString ( ) ) .csv "
function Process_AdminReport
{
2023-07-17 13:25:39 +05:30
$AdminMemberOf = Get-MgBetaUserTransitiveMemberOf -UserId $Admins . Id | Select-Object -ExpandProperty AdditionalProperties
2023-04-25 13:12:05 +05:30
$AssignedRoles = $AdminMemberOf | ? { $_ . '@odata.type' -eq '#microsoft.graph.directoryRole' }
$DisplayName = $Admins . DisplayName
if ( $Admins . AssignedLicenses -ne $null )
{
$LicenseStatus = " Licensed "
}
else
{
$LicenseStatus = " Unlicensed "
}
if ( $Admins . AccountEnabled -eq $true )
{
$SignInStatus = " Allowed "
}
else
{
$SignInStatus = " Blocked "
}
Write-Progress -Activity " Currently processing: $DisplayName " -Status " Updating CSV file "
if ( $AssignedRoles -ne $null )
{
$ExportResult = @ { 'Admin EmailAddress' = $Admins . mail ; 'Admin Name' = $DisplayName ; 'Assigned Roles' = ( @ ( $AssignedRoles . displayName ) -join ',' ) ; 'License Status' = $LicenseStatus ; 'SignIn Status' = $SignInStatus }
$ExportResults = New-Object PSObject -Property $ExportResult
$ExportResults | Select-Object 'Admin Name' , 'Admin EmailAddress' , 'Assigned Roles' , 'License Status' , 'SignIn Status' | Export-csv -path $OutputCsv -NoType -Append
}
2021-03-02 19:44:15 +05:30
}
2023-04-25 13:12:05 +05:30
function Process_RoleBasedAdminReport
{
2023-07-17 13:25:39 +05:30
$AdminList = Get-MgBetaDirectoryRoleMember -DirectoryRoleId $AdminRoles . Id | Select-Object -ExpandProperty AdditionalProperties
2023-04-25 13:12:05 +05:30
$RoleName = $AdminRoles . DisplayName
if ( $ExcludeGroups . IsPresent )
{
$AdminList = $AdminList | ? { $_ . '@odata.type' -eq '#microsoft.graph.user' }
$DisplayName = $AdminList . displayName
}
else
{
$DisplayName = $AdminList . displayName
}
if ( $DisplayName -ne $null )
{
Write-Progress -Activity " Currently Processing $RoleName role " -Status " Updating CSV file "
$ExportResult = @ { 'Role Name' = $RoleName ; 'Admin EmailAddress' = ( @ ( $AdminList . mail ) -join ',' ) ; 'Admin Name' = ( @ ( $DisplayName ) -join ',' ) ; 'Admin Count' = $DisplayName . Count }
$ExportResults = New-Object PSObject -Property $ExportResult
$ExportResults | Select-Object 'Role Name' , 'Admin Name' , 'Admin EmailAddress' , 'Admin Count' | Export-csv -path $OutputCsv -NoType -Append
}
}
2021-03-02 19:44:15 +05:30
#Check to generate role based admin report
if ( $RoleBasedAdminReport . IsPresent )
{
2023-07-17 13:25:39 +05:30
Get-MgBetaDirectoryRole -All | ForEach-Object {
2023-04-25 13:12:05 +05:30
$AdminRoles = $_
Process_RoleBasedAdminReport
}
2021-03-02 19:44:15 +05:30
}
#Check to get admin roles for specific user
elseif ( $AdminName -ne " " )
{
2023-04-25 13:12:05 +05:30
$AllUPNs = $AdminName . Split ( " , " )
ForEach ( $Admin in $AllUPNs )
{
2023-07-17 13:25:39 +05:30
$Admins = Get-MgBetaUser -UserId $Admin -ErrorAction SilentlyContinue
2023-04-25 13:12:05 +05:30
if ( $Admins -eq $null )
{
Write-host " $Admin is not available. Please check the input " -ForegroundColor Red
}
else
{
Process_AdminReport
}
}
2021-03-02 19:44:15 +05:30
}
#Check to get all admins for a specific role
elseif ( $RoleName -ne " " )
{
2023-04-25 13:12:05 +05:30
$RoleNames = $RoleName . Split ( " , " )
ForEach ( $Name in $RoleNames )
{
2023-07-17 13:25:39 +05:30
$AdminRoles = Get-MgBetaDirectoryRole -Filter " DisplayName eq ' $Name ' " -ErrorAction SilentlyContinue
2023-04-25 13:12:05 +05:30
if ( $AdminRoles -eq $null )
{
Write-Host " $Name role is not available. Please check the input " -ForegroundColor Red
}
else
{
Process_RoleBasedAdminReport
}
}
2021-03-02 19:44:15 +05:30
}
#Generating all admins report
else
2023-04-25 13:12:05 +05:30
{
2023-07-17 13:25:39 +05:30
Get-MgBetaUser -All | ForEach-Object {
2023-04-25 13:12:05 +05:30
$Admins = $_
Process_AdminReport
}
2021-03-02 19:44:15 +05:30
}
#Open output file after execution
2023-04-25 13:12:05 +05:30
if ( ( Test-Path -Path $OutputCsv ) -eq " True " )
{
2023-07-17 13:25:39 +05:30
Write-Host ` n " The Output file availble in: " -NoNewline -ForegroundColor Yellow ; Write-Host " $outputCsv " ` n
2023-04-25 13:12:05 +05:30
$prompt = New-Object -ComObject wscript . shell
$UserInput = $prompt . popup ( " Do you want to open output file? " , ` 0 , " Open Output File " , 4 )
If ( $UserInput -eq 6 )
{
Invoke-Item " $OutputCsv "
2023-07-17 13:25:39 +05:30
Write-Host " Report generated successfuly "
2023-04-25 13:12:05 +05:30
}
2021-03-02 19:44:15 +05:30
}
2023-04-25 13:12:05 +05:30
else
{
Write-Host " No data found " -ForegroundColor Red
}
2023-07-17 13:25:39 +05:30
Write-Host ` n ~ ~ Script prepared by AdminDroid Community ~ ~ ` n -ForegroundColor Green
Write-Host " ~~ Check out " -NoNewline -ForegroundColor Green ; Write-Host " admindroid.com " -ForegroundColor Yellow -NoNewline ; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~ " -ForegroundColor Green ` n ` n
2023-04-25 13:12:05 +05:30
Disconnect-MgGraph | Out-Null