Public/Remove-CollectionGroupLink.ps1

<#
.SYNOPSIS
    Removes collection group links in the Fortytwo Collections API.

.DESCRIPTION
    This function removes collection group links in the Fortytwo Collections API.

#>

function Remove-CollectionGroupLink {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(Mandatory = $true, ParameterSetName = "GroupLinkID", ValueFromPipeline = $true)]
        [ValidatePattern("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", ErrorMessage = "Id must be a valid GUID.")] # Is a guid connector object id
        [string] $GroupLinkID,

        [Parameter(Mandatory = $true, ParameterSetName = "CollectionID",ValueFromPipeline = $true)]
        [ValidatePattern("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", ErrorMessage = "Id must be a valid GUID.")] # Is a guid connector object id
        [string] $CollectionId
    )

    process {
        if (-not $Script:APIRoot -or -not $Script:AccessTokenProfile) {
            throw "Not connected to Fortytwo Collections. Please run Connect-Collections first."
        }

        # Get ID from the collection if the CollectionID parameter set is used
        if ($PSCmdlet.ParameterSetName -eq "CollectionID") {
            Write-Verbose "Retrieving collection group link by collection ID: $CollectionId"
            $Result = Get-CollectionGroupLinkByCollection -Id $CollectionId
            if ($Result.id) {
                $Id = $Result.id
            } else {
                throw "No collection group link found for collection ID: $CollectionId"
            }
        } else {
            $Id = $GroupLinkID
        }


        # Use the ID from the collection if it was retrieved above, if not use the provided GroupLinkID

        # Create the URI
        $Uri =  "$Script:APIRoot" -f "group-link", "/group-links/$Id"

        Write-Verbose "Removing collection group link. URI: $Uri"
        try {

            $Result = Invoke-RestMethod -Uri $Uri -Headers (Get-CollectionHeader) -Method Delete
            
            if ($Result.statusCode -eq 200) {
                Write-Verbose "Collection group link removed successfully."
            } elseif ($Result.statusCode -like "4*") {
                throw "Failed to remove collection group link. Status code: $($Result.statusCode)"
            }
        }
        catch {
            throw "Failed to remove collection group link. $_"
        }
    }
}