FunctionsPublic/Get-GraphClientAppAssignment.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<#
.SYNOPSIS
Get the configured Intune Client Apps assignments
 
.DESCRIPTION
Retrieves all the configured Client Apps assignments within Intune
 
.PARAMETER accessToken
A Microsoft Graph API access token with the required permissions
 
.PARAMETER clientAppID
The client app ID to get the assignments for
 
.PARAMETER nextLink
Next link if paging is required
#>

function Get-GraphClientAppAssignment
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)][psobject]$accessToken, 
        [parameter(Mandatory=$true)][string]$clientAppID, 
        [parameter(Mandatory=$false)][string]$nextLink
    )
    #
    # Get all existing groups
    #
    if($nextLink.Length -eq 0)
    {
        $responseBody = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/deviceAppManagement/mobileApps/$($clientAppID)/assignments" -Headers @{"Authorization" = "Bearer $($accessToken.AccessTokenCredential.GetNetworkCredential().password)"}
    }
    else
    {    
        $responseBody = Invoke-RestMethod -Uri $nextLink -Headers @{"Authorization" = "Bearer $($accessToken.AccessTokenCredential.GetNetworkCredential().password)"}
    }
    
    if($null -eq $responseBody.id)
    {
        $responseResult = $responseBody.value
    }
    else
    {
        $responseResult = $responseBody
    }

    #
    # Retrieve additional results when there are additional results available
    #
    if($responseBody.'@odata.nextLink'.Length -gt 0)
    {
        $responseResult += Get-GraphClientAppAssignment -accessToken $accessToken -nextLink $responseBody.'@odata.nextLink'
    }

    return $responseResult
}