Samples/AssignUpdateprofileForClient.ps1

# This script demonstrates how to update a property on multiple endpoints using a PATCH operation.
# It assigns a specific update profile to all endpoints within a given logical group.

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

# --- Configuration ---
$logicalGroupName = "YourLogicalGroup"
$updateProfileID = "YourUpdateprofileID"

try {
    # Get the logical group object using Select-bCPageData to get the item directly.
    $logicalGroup = Get-bCEndpointsLogicalGroups -Name $logicalGroupName | Select-bCPageData
    $logicalGroupID = $logicalGroup.id
    Write-Information "Found logical group '$($logicalGroup.name)' with ID: $logicalGroupID"

    # Get all endpoints from the logical group using the -GetAllPages switch and Select-bCPageData helper.
    # This avoids manual page handling.
    $endpointsInGroup = Get-bCEndpointsEndpointsByLogicalGroupId -LogicalGroupId $logicalGroupID -GetAllPages | Select-bCPageData
    Write-Information "Found $($endpointsInGroup.Count) endpoints to update."

    # Create the PATCH operation blueprint. This defines the change we want to apply.
    # This only needs to be done once.
    # The -Path parameter specifies the name of the property to change, prefixed with a '/'.
    # In this case, we are targeting the 'updateProfileId' property of the endpoint.
    $operation = Initialize-bCPatchOperation -Value $updateProfileID -Path "/updateProfileId" -Op "replace"

    # Iterate through the list of endpoints and apply the update.
    foreach($endpoint in $endpointsInGroup){
        Write-Information "Assigning update profile to endpoint '$($endpoint.displayName)'..."
        # Assign the update profile to the endpoint using its ID and the patch operation.
        Update-bCUpdatemanagementWindowsEndpoint -Id $endpoint.id -Operation $operation
    }

    Write-Output "Successfully assigned update profile to all endpoints in logical group '$logicalGroupName'."
}
catch {
    Write-Error "An error occurred: $($_.Exception.Message):`n$($_.ScriptStackTrace)"
}