Public/Curanet/M365/Get-CuranetM365Users.ps1

function Get-CuranetM365Users() {
    param(
        [Parameter(Mandatory)]
        [string] $CustomerId
    )

    Connect-CuranetM365API -CustomerId $CustomerId
    $Response = Invoke-WebRequest -Method GET -Authentication Bearer -Token (ConvertTo-SecureString $Global:CuranetM365APIToken.access_token -AsPlainText -Force) -Uri "https://office365.curanet.dk/api/user/"

    if($Response.StatusCode -eq 200) {
        $JsonResponse = $Response.Content | ConvertFrom-Json
        $LicenseUri = "https://office365.curanet.dk/api/license/bulk?"
        foreach($User in $JsonResponse.users) {
            $LicenseUri += "userIds=$($User.id)&"
        }

        $UserLicenses = ((Invoke-WebRequest -Method GET -Authentication Bearer -Token (ConvertTo-SecureString $Global:CuranetM365APIToken.access_token -AsPlainText -Force) -Uri $LicenseUri).Content | ConvertFrom-Json).userLicenses[0]
        foreach($User in $JsonResponse.users) {
            $User | Add-Member -MemberType NoteProperty -Name isLicensed -Value $false
            if($UserLicenses["$($User.id)"]) {
                $User.isLicensed = $true
            }
            else {
                $User.isLicensed = $false
            }
            Write-Host "User: $($User.displayName) is licensed: $($User.isLicensed)"
        }

        #return $JsonResponse.users
    }
    else {
        throw "Failed to get M365 users"
    }
}