Samples/RemoveCreatedEndpoints.ps1

# This script demonstrates how to manually handle paged API responses.
# It retrieves all endpoints from a specific logical group by iterating through each page of results,
# and then deletes each endpoint.

$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"

$logicalGroupName = "YourLogicalGroup"
$pageSize = 3

try{
    # Get the logical group object. We assume the name is unique.
    $logicalGroup = Get-bCEndpointsLogicalGroups -Name $logicalGroupName

    # Retrieve the logical group ID from the first result in the data array.
    $logicalGroupID = $logicalGroup.data[0].id
    if ($logicalGroupID)
    {
        Write-Output "Found Logical Group ID: $logicalGroupID"
    }
    else {
        Write-Error "Failed to find Logical Group with name '$logicalGroupName'"
    }

    # --- Manual Paging Example ---

    # 1. Make an initial API call to get the first page and discover the total number of pages.
    $firstPage = Get-bCEndpointsEndpointsByLogicalGroupId -LogicalGroupId $logicalGroupID -PageSize $pageSize

    # Array to store all endpoints collected from the pages.
    $endpointsList = @()

    # 2. Loop through all pages, from 1 to totalPages.
    for($i = 0; $i -lt $firstPage.totalPages; $i++){
        Write-Information "Fetching page $i of $($firstPage.totalPages)..."
        # Get the endpoints of the current page.
        $currentPage = Get-bCEndpointsEndpointsByLogicalGroupId -LogicalGroupId $logicalGroupID -Page $i -PageSize $pageSize

        # Add the endpoints from the current page to our list.
        foreach($endpointData in $currentPage.data){
            $endpointsList += $endpointData
        }
    }

    # 3. Process the collected endpoints.
    Write-Information "Found $($endpointsList.Count) endpoints to delete."
    foreach ($endpoint in $endpointsList) {
        # Remove the endpoint by its ID.
        Remove-bCEndpointsEndpoint -Id $endpoint.id

        Write-Output "Deleted endpoint $($endpoint.displayName)"
    }
}
catch {
    Write-Error "An error occurred: $($_.Exception.Message):`n$($_.ScriptStackTrace)"
}