Microsoft.Graph.API: Cmdlets for version 0.1.9

General

The module prefix changed from MSGraph to Mga to shorten the cmdlets as much as possible.


Removed cmdlets in version 0.1.9

Removed from module:

  • New-MSGraphPreGetRequest

Renamed Cmdlets in version 0.1.9

  • Connect-MSGraph -> Connect-Mga
  • Disconnect-MSGraph -> Disconnect-Mga
  • New-MSGraphGetRequest > Get-Mga
  • New-MsGraphPostRequest > Post-Mga
  • New-MSGraphPatchRequest > Patch-Mga
  • New-MSGraphDeleteRequest > Delete-Mga

New cmdlets in version 0.1.9

  • Batch-Mga
  • Send-MgaMail

Update Cmdlets in version 0.1.9

Connect-Mga

By selecting one of these parameters you log on with the following:

  • ClientSecret: Will log you on with a ClientSecret.
  • Thumbprint: Will log you on with a Certificate.
  • UserCredentials: Will log you on with basic authentication.
  • RedirectUri: Will log you on with MFA Authentication.

The OauthToken is automatically renewed when you use cmdlets.

If you want to know more about how to log in via MFA with a RedirectUri, follow this link.

Examples

Connect-Mga -ClientSecret '1yD3h~.KgROPO.K1sbRF~XXXXXXXXXXXXX' -applicationID 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' -Tenant 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' 

Connect-Mga -Thumbprint '3A7328F1059E9802FAXXXXXXXXXXXXXX' -applicationID 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' -Tenant 'XXXXXXXX.onmicrosoft.com' 

Connect-Mga -UserCredentials $Cred -Tenant 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' -ApplicationID 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX'

Connect-Mga -redirectUri 'msalXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX://auth' -Tenant 'XXXXXXXX.onmicrosoft.com'  -ApplicationID 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX'

Disconnect-Mga

To update the OauthToken I fill the global scope with a number of properties. The properties are emptied by Disconnect-Mga.

Examples

Disconnect-Mga

Get-Mga

Get-Mga speaks for itself. All you have to provide is the URL.

You can grab the URL via the browser developer tools, Fiddler, or from the Microsoft Graph docs. You can use all query parameters in the URL like some in the examples.

It will automatically use the Next Link when there is one in the returned request.

If you only want to retrieve data once, you can use the -Once parameter. For example, I used this in the beta version to get the latest login. Nowadays this property is with the user under signInActivity.

Examples

Get-Mga -URL 'https://graph.microsoft.com/v1.0/users' -Once

Get-Mga -URL 'https://graph.microsoft.com/v1.0/users?$top=999'

$URL = 'https://graph.microsoft.com/v1.0/users?$select={0}' -f 'id,userPrincipalName,lastPasswordChangeDateTime,createdDateTime,PasswordPolicies' 
Get-Mga -URL $URL

$URL = 'https://graph.microsoft.com/beta/users?$filter=({0})&$select=displayName,userPrincipalName,createdDateTime,signInActivity' -f "UserType eq 'Guest'"
Get-Mga URL $URL

Post-Mga

Post-Mga can be seen as the ‘new’ Verb. With this cmdlet you create objects in AzureAD.

-InputObject will accept a PSObject or JSON.

The example below creates a new user.

Examples

$InputObject = @{
    accountEnabled    = 'true'
    displayName       = "Test User Post MSGraph"
    mailNickname      = "TestUserPostMSGraph"
    userPrincipalName = "[email protected]"
    passwordProfile   = @{
        forceChangePasswordNextSignIn = 'true'
        password                      = 'XXXXXXXXXX'
    }
}
Post-Mga -URL 'https://graph.microsoft.com/v1.0/users' -InputObject $InputObject

Patch-Mga

Patch-Mga can be seen as the ‘Update’ Verb.

-InputObject will accept a PSObject or JSON.

InputObject with [email protected] property over 20+ users will automatically be batched.

In the below example I add users to a Group.

Examples

$users = Get-Mga 'https://graph.microsoft.com/v1.0/users'
$UserPostList = [System.Collections.Generic.List[Object]]::new() 
foreach ($User in $users)
{
    $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/4c9d31a2-c662-4f76-b3f8-52290d2aa788' -InputObject $PostBody 

Delete-Mga

Delete speaks for itself. With this cmdlet you can remove objects from AzureAD.

-URL accepts an array of URLS, it will use Batch-Mga in the backend.

-InputObject will accept a PSObject or JSON.

Examples

Delete-Mga -URL 'https://graph.microsoft.com/v1.0/users/[email protected]' 

Batch-Mga

Batch-Mga is espcially for speed and bulk.

Go to Speed or Bulk for more.

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.

Examples

$DeletedObjects = Get-Mga -URL 'https://graph.microsoft.com/v1.0/directory/deletedItems/microsoft.graph.user?$top=999'
$Batch = [System.Collections.Generic.List[Object]]::new()
foreach ($User in $DeletedObjects) {
    $Object = [PSCustomObject]@{
        url    = "/directory/deletedItems/$($User.id)"
        method = 'delete'
    }
    $Batch.Add($object)
}
Batch-Mga -InputObject $batch

Send-MgaMail

Send-MgaMail speaks for itself.

The -From addres can only be used when you connect with an App Registration.

Examples

Send-MgaMail -From '[email protected]' -To '[email protected]' -Subject 'Test message' -Body 'This is a test message'

Send-MgaMail -To '[email protected]' -Subject 'Test message' -Body 'This is a test message'

Previous version

Version 0.1.8