mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-17 16:35:19 +00:00
Export Office 365 Admin Report with MS Graph
Export Office 365 Admin Report with MS Graph
This commit is contained in:
parent
7b24f469c2
commit
362c5d5ca0
@ -1,163 +1,184 @@
|
|||||||
<#
|
<#
|
||||||
=============================================================================================
|
=============================================================================================
|
||||||
Name: Microsoft 365 Admin Report
|
Name: Export Microsoft 365 Admin Report using MS Graph PowerShell
|
||||||
Description: This script exports Microsoft 365 admin role group membership to CSV
|
Description: This script exports Microsoft 365 admin role group membership to CSV
|
||||||
Version: 1.0
|
Version: 3.0
|
||||||
website: o365reports.com
|
website: o365reports.com
|
||||||
Script by: O365Reports Team
|
Script by: O365Reports Team
|
||||||
For detailed Script execution: https://o365reports.com/2021/03/02/Export-Office-365-admin-role-report-powershell
|
For detailed Script execution: https://o365reports.com/2021/03/02/Export-Office-365-admin-role-report-powershell
|
||||||
============================================================================================
|
============================================================================================
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param (
|
param (
|
||||||
[string] $UserName = $null,
|
|
||||||
[string] $Password = $null,
|
|
||||||
[switch] $RoleBasedAdminReport,
|
[switch] $RoleBasedAdminReport,
|
||||||
|
[switch] $ExcludeGroups,
|
||||||
[String] $AdminName = $null,
|
[String] $AdminName = $null,
|
||||||
[String] $RoleName = $null)
|
[String] $RoleName = $null,
|
||||||
|
[string] $TenantId,
|
||||||
|
[string] $ClientId,
|
||||||
|
[string] $CertificateThumbprint
|
||||||
|
)
|
||||||
|
|
||||||
#Check for module availability
|
#Check for module availability
|
||||||
$msOnline = (get-module MsOnline -ListAvailable).Name
|
$MsGraphModule = Get-Module Microsoft.Graph -ListAvailable
|
||||||
if($msOnline -eq $null){
|
if($MsGraphModule -eq $null)
|
||||||
Write-host "Important: Module MsOnline 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 module? [Y] Yes [N] No
|
Write-host "Important: Microsoft graph module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
|
||||||
if($confirm -match "[yY]") {
|
$confirm = Read-Host Are you sure you want to install Microsoft graph module? [Y] Yes [N] No
|
||||||
Write-host "Installing MsOnline module..."
|
if($confirm -match "[yY]")
|
||||||
Install-Module MsOnline -Repository PsGallery -Force -AllowClobber
|
{
|
||||||
Write-host "Required Module is installed in the machine Successfully" -ForegroundColor Magenta
|
Write-host "Installing Microsoft graph module..."
|
||||||
} elseif($confirm -cnotmatch "[yY]" ){
|
Install-Module Microsoft.Graph -Scope CurrentUser
|
||||||
Write-host "Exiting. `nNote: MsOnline module must be available in your system to run the script"
|
Write-host "Microsoft graph module is installed in the machine successfully" -ForegroundColor Magenta
|
||||||
Exit
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
|
Write-host "Exiting. `nNote: Microsoft graph module must be available in your system to run the script" -ForegroundColor Red
|
||||||
|
Exit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if(($TenantId -ne "") -and ($ClientId -ne "") -and ($CertificateThumbprint -ne ""))
|
||||||
#Importing Module by default will avoid the cmdlet unrecognized error
|
{
|
||||||
Import-Module MsOnline -Force
|
Connect-MgGraph -TenantId $TenantId -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -ErrorAction SilentlyContinue -ErrorVariable ConnectionError|Out-Null
|
||||||
Write-Host "Connecting to Office 365..."
|
if($ConnectionError -ne $null)
|
||||||
|
{
|
||||||
#Storing credential in script for scheduling purpose/Passing credential as parameter
|
Write-Host $ConnectionError -Foregroundcolor Red
|
||||||
if(($UserName -ne "") -and ($Password -ne ""))
|
Exit
|
||||||
{
|
}
|
||||||
$securedPassword = ConvertTo-SecureString -AsPlainText $Password -Force
|
}
|
||||||
$credential = New-Object System.Management.Automation.PSCredential $UserName,$securedPassword
|
else
|
||||||
Connect-MsolService -Credential $credential | Out-Null
|
{
|
||||||
}
|
Connect-MgGraph -Scopes "Directory.Read.All" -ErrorAction SilentlyContinue -Errorvariable ConnectionError |Out-Null
|
||||||
else
|
if($ConnectionError -ne $null)
|
||||||
{
|
{
|
||||||
Connect-MsolService
|
Write-Host "$ConnectionError" -Foregroundcolor Red
|
||||||
}
|
Exit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Write-Host "Microsoft Graph Powershell module is connected successfully" -ForegroundColor Green
|
||||||
|
Select-MgProfile beta
|
||||||
Write-Host "Preparing admin report..."
|
Write-Host "Preparing admin report..."
|
||||||
$admins=@()
|
$Admins=@()
|
||||||
$list = @()
|
$RoleList = @()
|
||||||
$outputCsv=".\AdminReport_$((Get-Date -format MMM-dd` hh-mm` tt).ToString()).csv"
|
$OutputCsv=".\AdminReport_$((Get-Date -format MMM-dd` hh-mm` tt).ToString()).csv"
|
||||||
|
function Process_AdminReport
|
||||||
function process_Admin{
|
{
|
||||||
$roleList= (Get-MsolUserRole -UserPrincipalName $admins.UserPrincipalName | Select-Object -ExpandProperty Name) -join ','
|
$AdminMemberOf=Get-MgUserMemberOf -UserId $Admins.Id |Select-Object -ExpandProperty AdditionalProperties
|
||||||
if($admins.IsLicensed -eq $true)
|
$AssignedRoles=$AdminMemberOf|?{$_.'@odata.type' -eq '#microsoft.graph.directoryRole'}
|
||||||
{
|
$DisplayName=$Admins.DisplayName
|
||||||
$licenseStatus = "Licensed"
|
if($Admins.AssignedLicenses -ne $null)
|
||||||
}
|
{
|
||||||
else
|
$LicenseStatus = "Licensed"
|
||||||
{
|
}
|
||||||
$licenseStatus= "Unlicensed"
|
else
|
||||||
}
|
{
|
||||||
if($admins.BlockCredential -eq $true)
|
$LicenseStatus= "Unlicensed"
|
||||||
{
|
}
|
||||||
$signInStatus = "Blocked"
|
if($Admins.AccountEnabled -eq $true)
|
||||||
}
|
{
|
||||||
else
|
$SignInStatus = "Allowed"
|
||||||
{
|
}
|
||||||
$signInStatus = "Allowed"
|
else
|
||||||
}
|
{
|
||||||
$displayName= $admins.DisplayName
|
$SignInStatus = "Blocked"
|
||||||
$UPN= $admins.UserPrincipalName
|
}
|
||||||
Write-Progress -Activity "Currently processing: $displayName" -Status "Updating CSV file"
|
Write-Progress -Activity "Currently processing: $DisplayName" -Status "Updating CSV file"
|
||||||
if($roleList -ne "")
|
if($AssignedRoles -ne $null)
|
||||||
{
|
{
|
||||||
$exportResult=@{'AdminEmailAddress'=$UPN;'AdminName'=$displayName;'RoleName'=$roleList;'LicenseStatus'=$licenseStatus;'SignInStatus'=$signInStatus}
|
$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= New-Object PSObject -Property $ExportResult
|
||||||
$exportResults | Select-Object 'AdminName','AdminEmailAddress','RoleName','LicenseStatus','SignInStatus' | Export-csv -path $outputCsv -NoType -Append
|
$ExportResults | Select-Object 'Admin Name','Admin EmailAddress','Assigned Roles','License Status','SignIn Status' | Export-csv -path $OutputCsv -NoType -Append
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function Process_RoleBasedAdminReport
|
||||||
function process_Role{
|
{
|
||||||
$adminList = Get-MsolRoleMember -RoleObjectId $roles.ObjectId #Email,DisplayName,Usertype,islicensed
|
$AdminList = Get-MgDirectoryRoleMember -DirectoryRoleId $AdminRoles.Id |Select-Object -ExpandProperty AdditionalProperties
|
||||||
$displayName = ($adminList | Select-Object -ExpandProperty DisplayName) -join ','
|
$RoleName=$AdminRoles.DisplayName
|
||||||
$UPN = ($adminList | Select-Object -ExpandProperty EmailAddress) -join ','
|
if($ExcludeGroups.IsPresent)
|
||||||
$RoleName= $roles.Name
|
{
|
||||||
Write-Progress -Activity "Processing $RoleName role" -Status "Updating CSV file"
|
$AdminList=$AdminList| ?{$_.'@odata.type' -eq '#microsoft.graph.user'}
|
||||||
if($displayName -ne "")
|
$DisplayName=$AdminList.displayName
|
||||||
{
|
}
|
||||||
$exportResult=@{'RoleName'=$RoleName;'AdminEmailAddress'=$UPN;'AdminName'=$displayName}
|
else
|
||||||
$exportResults= New-Object PSObject -Property $exportResult
|
{
|
||||||
$exportResults | Select-Object 'RoleName','AdminName','AdminEmailAddress' | Export-csv -path $outputCsv -NoType -Append
|
$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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#Check to generate role based admin report
|
#Check to generate role based admin report
|
||||||
if($RoleBasedAdminReport.IsPresent)
|
if($RoleBasedAdminReport.IsPresent)
|
||||||
{
|
{
|
||||||
Get-MsolRole | ForEach-Object {
|
Get-MgDirectoryRole -All| ForEach-Object {
|
||||||
$roles= $_ #$ObjId = $_.ObjectId;$_.Name
|
$AdminRoles= $_
|
||||||
process_Role
|
Process_RoleBasedAdminReport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Check to get admin roles for specific user
|
#Check to get admin roles for specific user
|
||||||
elseif($AdminName -ne "")
|
elseif($AdminName -ne "")
|
||||||
{
|
{
|
||||||
$allUPNs = $AdminName.Split(",")
|
$AllUPNs = $AdminName.Split(",")
|
||||||
ForEach($admin in $allUPNs)
|
ForEach($Admin in $AllUPNs)
|
||||||
{
|
{
|
||||||
$admins = Get-MsolUser -UserPrincipalName $admin -ErrorAction SilentlyContinue
|
$Admins=Get-MgUser -UserId $Admin -ErrorAction SilentlyContinue
|
||||||
if( -not $?)
|
if($Admins -eq $null)
|
||||||
{
|
{
|
||||||
Write-host "$admin is not available. Please check the input" -ForegroundColor Red
|
Write-host "$Admin is not available. Please check the input" -ForegroundColor Red
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
process_Admin
|
Process_AdminReport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Check to get all admins for a specific role
|
#Check to get all admins for a specific role
|
||||||
elseif($RoleName -ne "")
|
elseif($RoleName -ne "")
|
||||||
{
|
{
|
||||||
$RoleNames = $RoleName.Split(",")
|
$RoleNames = $RoleName.Split(",")
|
||||||
ForEach($name in $RoleNames)
|
ForEach($Name in $RoleNames)
|
||||||
{
|
{
|
||||||
$roles= Get-MsolRole -RoleName $name -ErrorAction SilentlyContinue
|
$AdminRoles= Get-MgDirectoryRole -Filter "DisplayName eq '$Name'" -ErrorAction SilentlyContinue
|
||||||
if( -not $?)
|
if($AdminRoles -eq $null)
|
||||||
{
|
{
|
||||||
Write-Host "$name role is not available. Please check the input" -ForegroundColor Red
|
Write-Host "$Name role is not available. Please check the input" -ForegroundColor Red
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
process_Role
|
Process_RoleBasedAdminReport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Generating all admins report
|
#Generating all admins report
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Get-MsolUser -All | ForEach-Object {
|
Get-MgUser -All | ForEach-Object {
|
||||||
$admins= $_
|
$Admins= $_
|
||||||
process_Admin
|
Process_AdminReport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
write-Host "`nThe script executed successfully"
|
|
||||||
|
|
||||||
#Open output file after execution
|
#Open output file after execution
|
||||||
if((Test-Path -Path $outputCsv) -eq "True") {
|
if((Test-Path -Path $OutputCsv) -eq "True")
|
||||||
Write-Host "The Output file availble in $outputCsv" -ForegroundColor Green
|
{
|
||||||
$prompt = New-Object -ComObject wscript.shell
|
Write-Host "The Output file availble in $outputCsv" -ForegroundColor Green
|
||||||
$userInput = $prompt.popup("Do you want to open output file?",` 0,"Open Output File",4)
|
$prompt = New-Object -ComObject wscript.shell
|
||||||
If ($userInput -eq 6)
|
$UserInput = $prompt.popup("Do you want to open output file?",` 0,"Open Output File",4)
|
||||||
{
|
If ($UserInput -eq 6)
|
||||||
Invoke-Item "$OutputCSV"
|
{
|
||||||
}
|
Invoke-Item "$OutputCsv"
|
||||||
|
Write-Host "Report generated successfuly" -ForegroundColor Green
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Write-Host "No data found" -ForegroundColor Red
|
||||||
|
}
|
||||||
|
Disconnect-MgGraph|Out-Null
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user