Skip to content

PowerShell module for Microsoft Graph REST API. To optimize, speed, and bulk use Microsoft Graph API in PowerShell. You can can enter your own URL so you aren't restricted to the limitations of the official Microsoft Module. Includes ways to speed up the process, handle throttling, and re-authenticate after the token expires.

Notifications You must be signed in to change notification settings

baswijdenes/Optimized.Mga

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Optimized.Mga

PowerShell Gallery PowerShell Gallery

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?


CHANGELOG


Submodules dependent on Optimized.Mga:


Optimized.Mga Cmdlets


Are you new with the Microsoft Graph API?


What makes your module different from the official Microsoft Graph SDK for PowerShell?


Speed

The main difference is speed.

Group-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 {
            New-Mga -URL 'https://graph.microsoft.com/v1.0/users' -Body $User
        }
        catch {
            continue
        }
    }
}

Minutes           : 41
Seconds           : 6
Milliseconds      : 717

When I create the same users via Group-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 {
    Group-Mga -Body $Batch
}

Minutes           : 9
Seconds           : 43
Milliseconds      : 152

Group-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 Group-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

Set-Mga with parameters -Body 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
}

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

Same goes for Remove-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
}
Remove-Mga -URL $UserList

About

PowerShell module for Microsoft Graph REST API. To optimize, speed, and bulk use Microsoft Graph API in PowerShell. You can can enter your own URL so you aren't restricted to the limitations of the official Microsoft Module. Includes ways to speed up the process, handle throttling, and re-authenticate after the token expires.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published