From Microsoft.Graph.API to Optimized.Mga to fit the theme better: Speed, Bulk, and usability

Optimized.Mga > Microsoft.Graph.API

The name Microsoft.Graph.API is too similar to that of the official Microsoft SDK for PowerShell.
My module is neither better nor worse, but it depends on the situation in which you want to use it.


So when should I use your module for Microsoft Graph?

Don’t you wish you have a Microsoft Graph module which handles batching, the token and throttling for you, but where you can just enter your own URL so you aren’t restricted to the limitations of the official Microsoft Module and even includes a way to speed up the process?


Optimized.Mga: Version 0.1

Below is a copy of version 0.1. readme file. For the current version it is best to go to the Github main page.


Speed

The main difference is speed.

Batch-Mga doesn’t lie. When I use Measure-Command while creating 10,000 users via the Post command it takes about 41 minutes:

$CreatedUsers.count
10000
Measure-Command {
    foreach ($User in $CreatedUsers) {
        try {
            Post-Mga -URL 'https://graph.microsoft.com/v1.0/users' -InputObject $User
        }
        catch {
            continue
        }
    }
}

Minutes           : 41
Seconds           : 6
Milliseconds      : 717

When I create the same users via Batch-Mga, it’s 10 minutes:

$Batch = [System.Collections.Generic.List[Object]]::new()
foreach ($User in $CreatedUsers) {
    $Object = [PSCustomObject]@{
        Url    = "/users"
        method = 'post'
        body   = $User
    }
    $Batch.Add($Object)
}
Measure-Command {
    Batch-Mga -InputObject $Batch
}

Minutes           : 9
Seconds           : 43
Milliseconds      : 152

Batch-Mga will take care of the limitations (20 requests per batch) and will sleep for the amount of time a throttle limit is returned and then continue.


Usability

The second difference is usability. If you look at the official module you will see 33 dependencies. I made my module so that you only need 8 cmdlets.

The main cmdlet is of course Batch-Mga, by using Fiddler, or the browser developer tools you can find the URL when navigating through AzureAD and use it in one of the cmdlets.

For example the below URL is from the Intune Management GUI and found with Fiddler. It will get the Windows compliant devices and will only select the ComplianceState and UserPrincipalname.

$URL = 'https://graph.microsoft.com/beta/deviceManagement/managedDevices?$filter={0}&$top=999&$select=userPrincipalName,complianceState' -f "complianceState%20eq%20'Compliant'%20and%20operatingSystem%20eq%20'Windows'"
Get-Mga -URL $URL

Bulk

Patch-Mga with parameters -InputObject and -Batch and with the Property [email protected] will automatically be batched. So, in theory you can add 10000 users to a Group instantly. While throttling is handled for you.

$CreatedUsers = Get-Mga -URL 'https://graph.microsoft.com/v1.0/users?$top=999'
$UserPostList = [System.Collections.Generic.List[Object]]::new() 
foreach ($User in $CreatedUsers)
{
    $DirectoryObject = 'https://graph.microsoft.com/v1.0/directoryObjects/{0}' -f $User.id
    $UserPostList.Add($DirectoryObject)
}
$PostBody = [PSCustomObject] @{
    "[email protected]" = $UserPostList
}

Patch-Mga -URL 'https://graph.microsoft.com/v1.0/groups/ac252320-4194-402f-8182-2d14e4a2db5c' -InputObject $PostBody -Verbose

Same goes for Delete-Mga. When parameter -URL is an Array, it will automatically batch your request:

$Groupusers = Get-Mga -URL 'https://graph.microsoft.com/v1.0/groups/ac252320-4194-402f-8182-2d14e4a2db5c/members'
$UserList = @()
foreach ($User in $Groupusers) {
    $URL = 'https://graph.microsoft.com/v1.0/groups/ac252320-4194-402f-8182-2d14e4a2db5c/members/{0}/$ref' -f $User.Id
    $UserList += $URL
}
Delete-Mga -URL $UserLis


Are you new with the Microsoft Graph API?

Leave a Reply

Your email address will not be published. Required fields are marked *