Last active
October 16, 2024 18:06
-
-
Save scottd3v/a8f15e68b2bcc9aa4321653951306fbe to your computer and use it in GitHub Desktop.
Return all applications associated with a given conditional access policy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Org Specific Variables | |
| $OrgAPIKey = 'REPLACE-WITH-API-KEY' | |
| $AccessPolicyID = 'REPLACE-WITH-CAP-POLICY-ID' | |
| ### DO NOT MODIFY BELOW | |
| Connect-JCOnline -JumpCloudApiKey $OrgAPIKey -force | |
| # API endpoint and parameters | |
| $baseUrl = "https://console.jumpcloud.com/api/v2/applications" | |
| $limit = 100 # Number of items to retrieve per request | |
| $skip = 0 # Starting point for pagination | |
| $totalRecords = $null # Total records (if the API provides this in the response) | |
| ##### | |
| $headers = @{ | |
| "x-api-key" = "$OrgAPIKey" | |
| } | |
| # Function to make API requests and handle pagination | |
| function Get-PaginatedData { | |
| param ( | |
| [string]$url, | |
| [int]$limit, | |
| [int]$skip, | |
| [hashtable]$headers | |
| ) | |
| $paginatedData = @() | |
| $hasMoreData = $true | |
| while ($hasMoreData) { | |
| # Create the URL with skip and limit query parameters | |
| $requestUrl = "$($url)?skip=$skip&limit=$limit" | |
| # Make the API call | |
| $response = Invoke-RestMethod -Uri $requestUrl -Headers $headers -Method Get | |
| # Check if response contains data | |
| if ($response) { | |
| # Append response data to the result | |
| $paginatedData += $response # Assuming API returns a 'data' field with the actual resources | |
| # Increment the skip value by limit to get the next page | |
| $skip += $limit | |
| # Check if there's more data to fetch (adjust logic based on your API response) | |
| if ($response.data.Count -lt $limit) { | |
| $hasMoreData = $false | |
| } | |
| } else { | |
| # If no data is returned, stop pagination | |
| $hasMoreData = $false | |
| } | |
| } | |
| return $paginatedData | |
| } | |
| # Call the function to get all paginated app data | |
| $allData = Get-PaginatedData -url $baseUrl -limit $limit -skip $skip -headers $headers | |
| $allAppsTable = @{} | |
| foreach ($obj in $allData) { | |
| # Add to the hashtable where the key is the Name and the value is the ID | |
| $allAppsTable[$obj._id] = $obj.displayLabel | |
| } | |
| # Get access policy info | |
| $AceessPolicyObject = Get-JcSdkAuthenticationPolicy -id $AceessPolicyID | |
| $AppsInPolicy = @() | |
| foreach ($App in $AceessPolicyObject.TargetResources) { | |
| $myObject = [PSCustomObject]@{ | |
| ID = $App.id | |
| Name = $allAppsTable["$($App.id)"] | |
| } | |
| $AppsInPolicy += $myObject | |
| } | |
| $AppsInPolicy | |
| # view all apps by expanding the $allAppsTable variable | |
| $OrgAPIKey = 'jca_2AFPuhf59j1Pywz72LpSKf5oEp7rLyFVBay2' | |
| $AceessPolicyID = '670fea602023140001f3e07e' | |
| ## Org Specific Variables | |
| $OrgAPIKey = 'jca_2AFPuhf59j1Pywz72LpSKf5oEp7rLyFVBay2' | |
| $AccessPolicyID = '670fea602023140001f3e07e' | |
| ### DO NOT MODIFY BELOW | |
| # API endpoint and parameters | |
| $baseUrl = "https://console.jumpcloud.com/api/v2/applications" | |
| $limit = 100 # Items per request | |
| $skip = 0 # Starting point for pagination | |
| $headers = @{ "x-api-key" = $OrgAPIKey } | |
| # Function to fetch paginated data | |
| function Get-PaginatedData { | |
| param ( | |
| [string]$url, | |
| [int]$limit, | |
| [int]$skip, | |
| [hashtable]$headers | |
| ) | |
| $paginatedData = @() | |
| while ($true) { | |
| $requestUrl = "$($url)?skip=$skip&limit=$limit" | |
| $response = Invoke-RestMethod -Uri $requestUrl -Headers $headers -Method Get | |
| if ($response) { | |
| $paginatedData += $response | |
| $skip += $limit | |
| if ($response.data.Count -lt $limit) { break } | |
| } else { break } | |
| } | |
| return $paginatedData | |
| } | |
| # Retrieve all app data | |
| $allData = Get-PaginatedData -url $baseUrl -limit $limit -skip $skip -headers $headers | |
| # Create hashtable of apps | |
| $allAppsTable = @{} | |
| foreach ($obj in $allData) { | |
| $allAppsTable[$obj._id] = $obj.displayLabel | |
| } | |
| # Get access policy info | |
| $AccessPolicyObject = Get-JcSdkAuthenticationPolicy -id $AccessPolicyID | |
| # Prepare list of apps in the policy | |
| $AppsInPolicy = @() | |
| foreach ($App in $AccessPolicyObject.TargetResources) { | |
| $AppsInPolicy += [PSCustomObject]@{ | |
| ID = $App.id | |
| Name = $allAppsTable[$App.id] | |
| } | |
| } | |
| $AppsInPolicy # Output the list of apps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment