Azure Active Directory Assessment | Part III

Header tenantassesment
Reading Time: 5 minutes

In the previous post, Azure Active Directory Assessment | Part II, an App registration with the correct permissions was created. Via PowerShell the connection details where saved in a XML file on the desktop. Via the portal the details needed to be written down.

In this post, the assessment will start with Users and there properties. After that the Groups will be exported included the members and sharepoint/teams url and at last the Devices will be exported.

Connection

After the previous part, $ConnectionDetails is filled with the necessary information for the connection to Graph API.

# retrieve an access token
do {
    try{  
        $accessToken = (Get-MSALToken -Clientid $ConnectionDetails.ClientId -ClientSecret $connectionDetails.ClientSecret -TenantId $ConnectionDetails.TenantId).AccessToken
        $mustRetry = 0
    }
    catch {
        $webError = $_
        $mustRetry = 1
        Start-Sleep -seconds 2
    }
    
} while (
    $mustRetry -eq 1
)

When the token is aquired, build the Header.

$header = @{
    'Authorization' = "BEARER $accesstoken"
    'Content-type'  = "application/json"
}

Function for Running query to Graph API

function RunQueryAndProcess {
    Try {
        $Results = (Invoke-RestMethod -Headers $header  -Uri $url -Method Get)
    }
    catch {
        $webError = $_
        $mustRetry = 1
    }
    # if token is expired renew token
    If ($mustRetry -and ($weberror.ErrorDetails.message -like "*Access token has expired or is not yet valid.*")) {
        #region connection
        # Get an access token for the Microsoft Graph API
        do {
            Try {
                $accessToken = (Get-MSALToken -Clientid $ConnectionDetails.ClientId -ClientSecret $connectionDetails.ClientSecret -TenantId $ConnectionDetails.TenantId -ForceRefresh).AccessToken
                $header = @{
                    'Authorization' = "BEARER $accesstoken"
                    'Content-type'  = "application/json"
                }
            }
            Catch {
                write-host "Unable to acquire access token, check the parameters are correct`n$($Error[0])"
                exit
            }            
            Start-Sleep -seconds 2
        } while (
            $AccessToken -eq $null
        )
        
        #endregion connection
        $Results = (Invoke-RestMethod -Headers $header  -Uri $url -Method Get)
    }
    #Output Results for Debugging
    #write-host $results

    #Begin populating results
    [array]$ResultsValue = $Results.value

    #If there is a next page, query the next page until there are no more pages and append results to existing set
    if ($results."@odata.nextLink" -ne $null) {
        $NextPageUri = $results."@odata.nextLink"
        ##While there is a next page, query it and loop, append results
        While ($NextPageUri -ne $null) {
            $NextPageRequest = (Invoke-RestMethod -Headers $header -Uri $NextPageURI -Method Get)
            $NxtPageData = $NextPageRequest.Value
            $NextPageUri = $NextPageRequest."@odata.nextLink"
            $ResultsValue = $ResultsValue + $NxtPageData
        }
    }


    ##Return completed results
    return $ResultsValue
}

Users

When getting information about the users in Azure Active Directory, you need to determine which information you would like to retrieve.
In this post we have chosen for the following attributes:

  • ID
  • Status
  • Type
  • lastSignInActivity
  • UserPrincipalName
  • DisplayName
  • createdDateTime
  • lastPasswordChangeDateTime
  • GivenName
  • SurName
  • employeeHireDate
  • employeeId
  • employeeType
  • JobTitle
  • Company
  • Manager
  • licenseSkuId
  • licenseSkuPartNumber
  • licenseServicePlan
  • Usage Location
  • mailNickname
  • proxyAddresses
  • OnPremisesSyncEnabled
  • OnPremisesLastSyncDateTime
  • OnPremisesDistinguishedName
  • OnPremisesImmutableId
  • OnPremisesSamAccountName
  • OnPremisesDomainName
  • OnPremisesUserPrincipalName
  • OnPremisesSecurityIdentifier
  • mailbox statistics

To achieve this information in a single export, we need to run some query’s and combine those to the end result. Let’s start with the easy information.

# setting url
$url = "https://graph.microsoft.com/v1.0/users?`$select=lastPasswordChangeDateTime,signInActivity,createdDateTime,licenseAssignmentStates,id,AccountEnabled,UserprincipalName,proxyAddresses,UserType,DisplayName,GivenName,Surname,EmployeeHireDate,EmployeeID,EmployeeType,Identities,JobTitle,Company,manager,usagelocation,mailnickname,onpremisesdistinguishedname,onpremisesdomainname,OnPremisesImmutableId,OnPremisesLastSyncDateTime,OnPremisesSamAccountName,OnPremisesSecurityIdentifier,OnPremisesSyncEnabled,OnPremisesUserPrincipalName"

# getting the users
$Users = RunQueryAndProcess

# adding additional placeholders to array
foreach ($user in $users) {
    $user.proxyaddresses = $user.proxyaddresses -join ';'
    $user | Add-Member -MemberType NoteProperty -Name "License SKUs" -Value ($user.licenseAssignmentStates.skuid -join ";") -Force
    $user | Add-Member -MemberType NoteProperty -Name "Group License Assignments" -Value ($user.licenseAssignmentStates.assignedByGroup -join ";") -Force
    $user | Add-Member -MemberType NoteProperty -Name "Disabled Plan IDs" -Value ($user.licenseAssignmentStates.disabledplans -join ";") -Force
    $user | Add-Member -MemberType NoteProperty -Name "Mailbox Items" -Value "" -force
    $user | Add-Member -MemberType NoteProperty -Name "Mailbox isDeleted" -Value "" -force
    $user | Add-Member -MemberType NoteProperty -Name "Last Activity Date" -Value "" -force
    $user | Add-Member -MemberType NoteProperty -Name "Storage Used" -Value "" -force
    $user | Add-Member -MemberType NoteProperty -Name "Deleted Item Count" -Value "" -force
    $user | Add-Member -MemberType NoteProperty -Name "Deleted Item Size" -Value "" -force
    $user | Add-Member -MemberType NoteProperty -Name "Archive" -Value "" -force
}

# getting mailbox reports
$url = "https://graph.microsoft.com/v1.0/reports/getMailboxUsageDetail(period='D30')"
$url = "https://graph.microsoft.com/v1.0/reports/getMailboxUsageDetail(period='D30')"

try { 
    $MailboxStatsReport = ((Invoke-Restmethod -Headers $Header -Uri $url -Method GET) | ConvertFrom-Csv)}
catch {
    $webError = $_
    $mustRetry = 1
}
If ($mustRetry -and ($weberror.ErrorDetails.message -like "*Access token has expired or is not yet valid.*")) {
    #region connection
    # Get an access token for the Microsoft Graph API
    do {
        $accessToken = (Get-MSALToken -Clientid $ConnectionDetails.ClientId -ClientSecret $connectionDetails.ClientSecret -TenantId $ConnectionDetails.TenantId -ForceRefresh).AccessToken
        Start-Sleep -seconds 2
    } while (
        $accessToken -eq $null
    )
    $resource = "https://graph.microsoft.com/"
    $header = @{
        'Authorization' = "BEARER $accesstoken"
        'Content-type'  = "application/json"
    }
    #endregion connection
    $MailboxStatsReport = ((Invoke-Restmethod -Headers $Header -Uri $url -Method GET) | ConvertFrom-Csv)
}

# getting groups
$url = "https://graph.microsoft.com/v1.0/groups"
$Groups = RunQueryAndProcess

# getting licenses information
$url = "https://graph.microsoft.com/v1.0/subscribedskus"
$SKUs = RunQueryAndProcess

# processing data to user table
foreach ($user in $users) {
    foreach ($Group in $Groups) {
        $user.'Group License Assignments' = $user.'Group License Assignments'.replace($group.id, $group.displayName) 
    }
    foreach ($SKU in $SKUs) {
        $user.'License SKUs' = $user.'License SKUs'.replace($SKU.skuid, $SKU.skuPartNumber)
    }
    foreach ($SKUplan in $SKUs.servicePlans) {
        $user.'Disabled Plan IDs' = $user.'Disabled Plan IDs'.replace($SKUplan.servicePlanId, $SKUplan.servicePlanName)
    }

    $Stats = $null
    $stats = $MailboxStatsReport | Where-Object {$_.'User Principal Name' -eq $user.userPrincipalName}
    if ($Stats){
       $User.'Mailbox Items' = $Stats.'Item Count'
       $user.'Mailbox isDeleted' = $Stats.'Is Deleted'
       $user.'Last Activity Date' = $stats.'Last Activity Date'
       $user.'Storage Used (MB)' = $stats.'Storage Used (Byte)' /1024 /1024
       $user.'Storage Used (MB)'  = [math]::Round($user.'Storage Used (MB)' , 2)
       $user.'Deleted Item Count' = $Stats.'Deleted Item Count'
       $user.'Deleted Item Size (MB)' = $Stats.'Deleted Item Size (Byte)' /1024 /1024
       $user.'Deleted Item Size (MB)'  = [math]::Round($user.'Deleted Item Size (MB)' , 2)
       $user.Archive = $Stats.'Has Archive'
    }
}

Groups

There are different type of groups and some of them are also related to teams or sharepoint. To get a good overview over the groups, we need to get all the information there is about a group. With the Users we al ready pulled all the groups from GraphAPI. Now we need to check what is more behind.

# getting team groups
$TeamGroups = $Groups | Where-Object { ($_.grouptypes -Contains "unified") -and ($_.resourceProvisioningOptions -contains "Team") }

# getting all the TEAMS information for each group
foreach ($teamgroup in $TeamGroups) {

    $url = "https://graph.microsoft.com/beta/teams/$($teamgroup.id)/allchannels"
    $Teamchannels = RunQueryAndProcess
    $standardchannels = ($teamchannels | Where-Object  { $_.membershipType -eq "standard" })
    $privatechannels = ($teamchannels | Where-Object  { $_.membershipType -eq "private" })
    $outgoingsharedchannels = ($teamchannels | Where-Object  { ($_.membershipType -eq "shared") -and (($_."@odata.id") -like "*$($teamgroup.id)*") })
    $incomingsharedchannels = ($teamchannels | Where-Object  { ($_.membershipType -eq "shared") -and ($_."@odata.id" -notlike "*$($teamgroup.id)*") })
    $teamgroup | Add-Member -MemberType NoteProperty -Name "StandardChannels" -Value $standardchannels.id.count -Force
    $teamgroup | Add-Member -MemberType NoteProperty -Name "PrivateChannels" -Value $privatechannels.id.count -Force
    $teamgroup | Add-Member -MemberType NoteProperty -Name "SharedChannels" -Value $outgoingsharedchannels.id.count -Force
    $teamgroup | Add-Member -MemberType NoteProperty -Name "IncomingSharedChannels" -Value $incomingsharedchannels.id.count -Force
    $privatechannelSize = 0
    
    foreach ($Privatechannel in $privatechannels) {
        $PrivateChannelObject = $null
        $apiuri = "https://graph.microsoft.com/beta/teams/$($teamgroup.id)/channels/$($Privatechannel.id)/FilesFolder"
        Try {
            $PrivateChannelObject = RunQueryAndProcess
            $Privatechannelsize += $PrivateChannelObject.size
        }
        catch {
            $Privatechannelsize += 0
        }
    }

    $sharedchannelSize = 0
    
    foreach ($sharedchannel in $outgoingsharedchannels) {
        $sharedChannelObject = $null
        $apiuri = "https://graph.microsoft.com/beta/teams/$($teamgroup.id)/channels/$($Sharedchannel.id)/FilesFolder"
        Try {
            $SharedChannelObject = RunQueryAndProcess
            $Sharedchannelsize += $SharedChannelObject.size

        }
        Catch {
            $Sharedchannelsize += 0
        }
    }

    $teamgroup | Add-Member -MemberType NoteProperty -Name "PrivateChannelsSize" -Value $privatechannelSize -Force
    $teamgroup | Add-Member -MemberType NoteProperty -Name "SharedChannelsSize" -Value $sharedchannelSize -Force
    

    $TeamDetails = $null
    $url = "https://graph.microsoft.com/v1.0/groups/$($teamgroup.id)/drive/"
    Try {
        $TeamDetails = RunQueryAndProcess
    }
    catch {

    }
    $teamgroup | Add-Member -MemberType NoteProperty -Name "DataSize" -Value $TeamDetails.quota.used -Force
    if ($TeamDetails.weburl){
    $teamgroup | Add-Member -MemberType NoteProperty -Name "URL" -Value $TeamDetails.webUrl.replace("/Shared%20Documents", "") -Force -ErrorAction SilentlyContinue
    }
}

Now every group is checked if it is a teams group or not and gathered the information, we going to check who are the members of these groups

# creating a table for membership
$GroupMembersObject = @()

# getting group memberships
foreach ($group in $groups) {
    $url = "https://graph.microsoft.com/v1.0/groups/$($group.id)/members"
    $Members = RunQueryAndProcess

    foreach ($member in $members) {

        $MemberEntry = [PSCustomObject]@{
            GroupID                 = $group.id
            GroupName               = $group.displayname
            MemberID                = $member.id
            MemberName              = $member.displayname
            MemberUserPrincipalName = $member.userprincipalname
            MemberType              = "Member"
            MemberObjectType        = $member.'@odata.type'.replace('#microsoft.graph.', '')

        }
        $GroupMembersObject += $memberEntry
    }

    $url = "https://graph.microsoft.com/v1.0/groups/$($group.id)/owners"
    $Owners = RunQueryAndProcess

    foreach ($member in $Owners) {

        $MemberEntry = [PSCustomObject]@{
            GroupID                 = $group.id
            GroupName               = $group.displayname
            MemberID                = $member.id
            MemberName              = $member.displayname
            MemberUserPrincipalName = $member.userprincipalname
            MemberType              = "Owner"
            MemberObjectType        = $member.'@odata.type'.replace('#microsoft.graph.', '')

        }
        $GroupMembersObject += $memberEntry
    }
}

Devices

These days more and more devices are connected to Azure AD. Let’s inventory them.

# setting url
$url = "https://graph.microsoft.com/v1.0/devices?`$select=id,accountEnabled,DisplayName,OperatingSystem,Operaringsystemversion,profiletype,registrationDateTime,approximateLastSignInDateTime,complianceExpirationDateTime,enrollmentProfileName,enrollmentType,deviceOwnership,domainName,isCompliant,isManaged,isRooted,manufacturer,mdmAppid,model"


# getting device information
$Devices = RunQueryAndProcess

Share and Enjoy !

Shares
Designer (23)

Stay close to the action—follow GetToThe.Cloud across social!
Deep dives and hands‑on how‑tos on Azure Local, hybrid cloud, automation, PowerShell/Bicep, AVD + FSLogix, image pipelines, monitoring, networking, and resilient design when the internet/Azure is down.

🔗 Our channels
▶️ YouTube: https://www.youtube.com/channel/UCa33PgGdXt-Dr4w3Ub9hrdQ
💼 LinkedIn Group: https://www.linkedin.com/groups/9181126/
✖️ X (Twitter): https://x.com/Gettothecloud
🎵 TikTok: https://www.tiktok.com/@gettothecloud
🐙 GitHub: https://github.com/GetToThe-Cloud/Website
💬 Slack: DM us for an invite
📲 WhatsApp: DM for the community link

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Our website address is: https://www.gettothe.cloud

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection. An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year. If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser. When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed. If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website. These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue. For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where we send your data

Visitor comments may be checked through an automated spam detection service.
Save settings
Cookies settings