Public/Set-CollectionGroupLink.ps1
|
<# .SYNOPSIS Sets collection group links in the Fortytwo Collections API. .DESCRIPTION This function updates collection group links in the Fortytwo Collections API. #> function Set-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, [Parameter(Mandatory = $false)] [ValidateSet("Enabled", "Disabled")] [string] $state ) process { if (-not $Script:APIRoot -or -not $Script:AccessTokenProfile) { throw "Not connected to Fortytwo Collections. Please run Connect-Collections first." } # Prepare the body for the PATCH request by excluding the ID parameters $BodyParams = $PSBoundParameters.keys | Where-Object { $_ -notin @("GroupLinkID", "CollectionId", "Verbose", "Debug", "ErrorAction", "ErrorVariable", "WarningAction", "WarningVariable") } $Body = @{} foreach ($Param in $BodyParams) { $Body[$Param] = $PSBoundParameters[$Param] } # Ensure that there is at least one parameter to update if ($Body.Count -eq 0) { throw "No parameters provided to update." } # 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 "Setting collection group link state. URI: $Uri" Write-Debug ($Body | ConvertTo-Json -Depth 10) try { $Result = Invoke-RestMethod -Uri $Uri -Headers (GATH -Profile $PROFILE) -Method PATCH -Body ($Body | ConvertTo-Json -Depth 10) -ContentType "application/json" if ($Result.statusCode -eq 200) { Write-Verbose "Collection group link state set successfully." } elseif ($Result.statusCode -like "4*") { throw "Failed to set collection group link state. Status code: $($Result.statusCode)" } } catch { throw "Failed to set collection group link state. $_" } } } |