Samples/CreateEndpoints.ps1

# This script demonstrates how to create multiple Windows endpoints in a specific logical group.
# It showcases the common two-step creation pattern used in this module:
# 1. Use an `Initialize-...ForCreation` function to create a local PowerShell object that acts as a blueprint.
# 2. Pass this blueprint object to a `New-...` function to create the actual resource via the API.

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

# --- Configuration ---
$logicalGroupName = "YourLogicalGroup"
$displayNameEP = "YourEndpointName"
$hostNameEP = "YourHostName"
$amount = 30

try {
    # Check if the target logical group already exists.
    # Note: We use Select-bCPageData to get the group object directly.
    $logicalGroup = Get-bCEndpointsLogicalGroups -Name $logicalGroupName | Select-bCPageData

    # If the logical group is not found, create it.
    if (-not $logicalGroup) {
        Write-Information "Logical group '$logicalGroupName' not found. Creating it..."
        # Step 1: Create the local blueprint object for the new logical group.
        $newLogicalGroupBlueprint = Initialize-bCEndpointsLogicalGroupForCreation -Name $logicalGroupName

        # Step 2: Create the logical group using the blueprint.
        $logicalGroup = New-bCEndpointsLogicalGroup -LogicalGroupForCreation $newLogicalGroupBlueprint

        Write-Output "Created new logical group with name '$($logicalGroup.name)'"
    }

    $logicalGroupId = $logicalGroup.id

    # Loop to create the specified number of endpoints.
    for($i = 1; $i -le $amount; $i++){

        # Set a unique display name and host name for each new endpoint.
        $displayName = ($displayNameEP + $i)
        $hostName = ($hostNameEP + $i)

        # Step 1: Create the local blueprint for the new Windows endpoint.
        $newEndpointBlueprint = Initialize-bCEndpointsWindowsEndpointForCreation -DisplayName $displayName -HostName $hostName -LogicalGroupId $logicalGroupId -Domain "yourDomain"

        # Step 2: Create the Windows endpoint using the blueprint.
        $createdEndpoint = New-bCEndpointsWindowsEndpoint -WindowsEndpointForCreation $newEndpointBlueprint
        Write-Output "Created new Windows Endpoint with name '$($createdEndpoint.displayName)'"
    }
}
catch {
    Write-Error "An error occurred: $($_.Exception.Message):`n$($_.ScriptStackTrace)"
}