Export a Report On All Global Admins For All Partner Tenants

Recently I’ve needed to audit all Office 365 Global Admin accounts on our clients tenants, and I wrote up a quick script to leverage our indrect reseller relationship to our clients tenants to export a list of all global admins using one set of credentials.

$logPath = "C:\Temp\GlobalAdmins.csv"

$LiveCred = Get-Credential
Connect-MsolService -Credential $LiveCred

$Tenants = Get-MsolPartnerContract -All
$role = Get-MsolRole -RoleName "Company Administrator"

Foreach($Tenant in $Tenants){
    $curTenantInfo = Get-MsolCompanyInformation -TenantId $Tenant.TenantId
    #$GlobalAdmins = New-Object System.Collections.ArrayList
    $MSOLAdmins = Get-MsolRoleMember -TenantId $Tenant.TenantId -RoleObjectId $role.ObjectID
    Write-Host "Getting admins for tenant $($curTenantInfo.Displayname.ToString())" -ForegroundColor Yellow

    ForEach($admin in $MSOLAdmins){
        $MSOLUser = Get-MSOLUser -TenantId $Tenant.TenantId -ObjectId $admin.ObjectID
        Write-host $MSOLUser.UserPrincipalName -ForegroundColor Cyan
        Add-Content $logPath "$($admin.DisplayName),$($MSOLUser.UserPrincipalName),$($curTenantInfo.DisplayName),$($Tenant.TenantId)"