Samples/PagingHelper.ps1

# This script demonstrates how to handle paged API responses using the -GetAllPages switch and the Select-bCPageData helper function.
# It shows how to retrieve all items from a paged endpoint and process them in a pipeline.

# Sets Write-Information for console output
# PowerShell default is SilentlyContinue
$InformationPreference = "Continue"
$ErrorActionPreference = "Stop"

# --- Configuration ---
# Replace these placeholder values with your specific data.
$logicalGroupName = "YourLogicalGroup"
$displayNameEP = "yourEndpointName"
$operationPath = "/logicalGroupId"

try {
    # --- Example 1: Retrieving all pages ---
    # The -GetAllPages switch returns an object that contains all pages of the result set.
    # This is useful if you need to inspect page-level information.
    #
    # We use -SearchQuery as an example, use more specific filters like -DisplayName if available.
    # If your search condition is always the same, consider creating a universal dynamic group
    # and querying its results via Get-bCEndpointsWindowsEndpointsByUniversalDynamicGroupId.
    Write-Information "Example 1: Getting all pages for endpoints matching '$displayNameEP'"
    $allPossiblePages = Get-bCEndpointsWindowsEndpoints -GetAllPages -SearchQuery $displayNameEP -PageSize 10
    Write-Information "Total Pages Retrieved: $($allPossiblePages.Count)"

    # --- Example 2: Extracting all items from pages ---
    # The Select-bCPageData helper function extracts the actual data items from all pages.
    # This is the simplest way to get a flat list of all items.
    Write-Information "Example 2: Extracting all endpoint data using Select-bCPageData"
    $allEndpointData = Get-bCEndpointsWindowsEndpoints -GetAllPages -SearchQuery $displayNameEP | Select-bCPageData
    Write-Information "Total Endpoints Retrieved: $($allEndpointData.Count)"

    # --- Example 3: Piping and filtering paged data ---
    # This example demonstrates a complete pipeline:
    # 1. Get all pages of Windows endpoints.
    # 2. Extract all endpoint items.
    # 3. Filter for endpoints in the default logical group.
    # Filtering by logical group is just an example, in practice you would use Get-bCEndpointsWindowsEndpointsByLogicalGroupId for this purpose.
    Write-Information "Example 3: Finding endpoints in the default logical group"
    $endpointsInNoLogicalGroup = Get-bCEndpointsWindowsEndpoints -GetAllPages -SearchQuery $displayNameEP -PageSize 10  `
                                | Select-bCPageData `
                                | Where-Object {
                                    Write-Information "Processing Endpoint $($_.displayName)"
                                    return $_.logicalGroup -eq "Logical group"
                                }

    if (-not $endpointsInNoLogicalGroup) {
        Write-Warning "No endpoint found in the default logical group matching '$displayNameEP'."
    }

    # Get the target logical group object.
    # Using Select-bCPageData here ensures we get the data item, even if the result has only one page.
    $customLogicalGroup = Get-bCEndpointsLogicalGroups -Name $logicalGroupName | Select-bCPageData

    if (-not $customLogicalGroup) {
        Write-Error "Logical group '$logicalGroupName' not found."
    }

    # --- Example 4: Updating filtered endpoints ---
    # Change the logical group for the endpoints found in the previous step.
    Write-Information "Example 4: Updating logical group for filtered endpoints"
    foreach ($endpoint in $endpointsInNoLogicalGroup) {
        $operation = Initialize-bCPatchOperation -Op replace -Path $operationPath -Value $customLogicalGroup.id
        $updatedEndpoint = Update-bCEndpointsWindowsEndpoint -Id $endpoint.id -Operation $operation
        Write-Output "The logical group changed for endpoint $($updatedEndpoint.displayName) to $($updatedEndpoint.logicalGroup)"
    }
}
catch {
    Write-Error "An error occurred: $($_.Exception.Message):`n$($_.ScriptStackTrace)"
}