Giter Site home page Giter Site logo

baswijdenes / optimized.mga Goto Github PK

View Code? Open in Web Editor NEW
30.0 2.0 3.0 1.17 MB

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.

Home Page: https://github.com/baswijdenes/Optimized.Mga

PowerShell 100.00%
microsoft-graph-api powershell

optimized.mga's Introduction

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

optimized.mga's People

Contributors

baswijdenes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

optimized.mga's Issues

bad request when using batch-mga or error 400 when creating batch of users

Hi there,
i am wondering what am i doing wrong, trying to batch create users just like in your example but getting this error:

Batch-Mga -InputObject $batch
WARNING: WebException Error message! This could be due to throttling limit.
The remote server returned an error: (400) Bad Request.
At C:\Program Files\WindowsPowerShell\Modules\Optimized.Mga\0.0.2.3\Optimized.Mga.psm1:382 char:17

  •             throw $_.Exception.Message
    
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : OperationStopped: (The remote serv...0) Bad Request.:String) [], RuntimeException
    • FullyQualifiedErrorId : The remote server returned an error: (400) Bad Request.

my batch(3 objects example) looks like this:
Url : https://graph.microsoft.com/beta/users
method : post
body : {
"accountEnabled": "true",
"displayName": "GraphUser6000",
"userPrincipalName": "[email protected]",
"mailNickname": "GraphUser6000",
"passwordProfile": {
"password": "xWwvJ]6NMw+bWH-d",
"forceChangePasswordNextSignIn": "true"
}
}

Url : https://graph.microsoft.com/beta/users
method : post
body : {
"accountEnabled": "true",
"displayName": "GraphUser6001",
"userPrincipalName": "[email protected]",
"mailNickname": "GraphUser6001",
"passwordProfile": {
"password": "xWwvJ]6NMw+bWH-d",
"forceChangePasswordNextSignIn": "true"
}
}

Url : https://graph.microsoft.com/beta/users
method : post
body : {
"accountEnabled": "true",
"displayName": "GraphUser6002",
"userPrincipalName": "[email protected]",
"mailNickname": "GraphUser6002",
"passwordProfile": {
"password": "xWwvJ]6NMw+bWH-d",
"forceChangePasswordNextSignIn": "true"
}
}

thanks in advance

Receive-MgaOauthToken function ParameterSetName 'RedirectUri' and 'Redirecturi' character-case issue

Hi Bas,

i just tried connect-mga using the RedirectUri parameter. When doing so with the current version 0.0.2.7 I get the error that the parameterset could not be resolved. It turns out that the name used in the ParameterSetName is actually case-sensitive. In two places in the module-function Receive-MgaOauthToken the ParameterSetName is Redirecturi instead of RedirectUri.
After changing that case and reloading the module it works.

Rgds,
Eric

commands missing

I followed the directions at https://baswijdenes.com/get-azuread-guest-users-lastlogin-for-stale-accounts-powershell/#Which_property_comes_closest_to_the_LastLogin_property. When I use powershell in ISE with PS version 5.1.19041.1682, I do not see commands Get-AzureADUsers. I received the following error:
PS C:\WINDOWS\system32> Get-AzureADUsers
Get-AzureADUsers : The term 'Get-AzureADUsers' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1

  • Get-AzureADUsers
  •   + CategoryInfo          : ObjectNotFound: (Get-AzureADUsers:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    

I was able to run Connect-Mga -ApplicationID with params and do everything else. Looking forward to being able to get the last login for users. Please help!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.