Public/Get-AADgroup.ps1

<#
 
.COPYRIGHT
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
See https://github.com/microsoftgraph/powershell-intune-samples/blob/master/LICENSE for license information.
 
#>


Function Get-AADGroup() {
    
        <#
    .SYNOPSIS
    This function is used to get AAD Groups from the Graph API REST interface
    .DESCRIPTION
    The function connects to the Graph API Interface and gets any Groups registered with AAD
    .EXAMPLE
    Get-AADGroup
    Returns all users registered with Azure AD
    .NOTES
    NAME: Get-AADGroup
    #>

    
        [cmdletbinding()]
    
        param
        (
            $GroupName,
            $id,
            [switch]$Members
        )
    
        # Defining Variables
        $graphApiVersion = "v1.0"
        $Group_resource = "groups"
    
        try {
    
            if ($id) {
    
                $uri = "https://graph.microsoft.com/$graphApiVersion/$($Group_resource)?`$filter=id eq '$id'"
                (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
    
            }
    
            elseif ($GroupName -eq "" -or $GroupName -eq $null) {
    
                $uri = "https://graph.microsoft.com/$graphApiVersion/$($Group_resource)"
                (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
    
            }
    
            else {
    
                if (!$Members) {
    
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($Group_resource)?`$filter=displayname eq '$GroupName'"
                    (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
    
                }
    
                elseif ($Members) {
    
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($Group_resource)?`$filter=displayname eq '$GroupName'"
                    $Group = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
    
                    if ($Group) {
    
                        $GID = $Group.id
    
                        $Group.displayName
                                    
    
                        $uri = "https://graph.microsoft.com/$graphApiVersion/$($Group_resource)/$GID/Members"
                        (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
    
                    }
    
                }
    
            }
    
        }
    
        catch {
    
            $ex = $_.Exception
            $errorResponse = $ex.Response.GetResponseStream()
            $reader = New-Object System.IO.StreamReader($errorResponse)
            $reader.BaseStream.Position = 0
            $reader.DiscardBufferedData()
            $responseBody = $reader.ReadToEnd();
            Write-Host "Response content:`n$responseBody" -f Red
            Write-Error "Request to $Uri failed with HTTP Status $($ex.Response.StatusCode) $($ex.Response.StatusDescription)"
            
            break
    
        }
    
    }