Private/Invoke-CloudResourceUpdate.ps1

function Invoke-CloudResourceUpdate {
    <#
    .SYNOPSIS
        Private helper function to handle resource updates with confirmation.
     
    .DESCRIPTION
        Centralizes the ShouldProcess logic and update workflow for cloud resources.
        Used for PATCH operations like changing ownership, permissions, etc.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCmdlet]$CallerPSCmdlet,
        
        [Parameter(Mandatory = $true)]
        [string]$ResourceType,
        
        [Parameter(Mandatory = $true)]
        [int]$ID,
        
        [Parameter(Mandatory = $true)]
        [string]$Uri,
        
        [Parameter(Mandatory = $true)]
        [object]$Body,
        
        [Parameter(Mandatory = $true)]
        [string]$Action,
        
        [Parameter(Mandatory = $false)]
        [scriptblock]$GetResourceScript,
        
        [Parameter(Mandatory = $false)]
        [string]$SuccessMessage
    )
    
    # Try to get resource details for a better confirmation message
    $resourceName = "Unnamed"
    if ($GetResourceScript) {
        try {
            $resource = & $GetResourceScript
            if ($resource -and $resource.name) {
                $resourceName = $resource.name
            }
        }
        catch {
            Write-Warning "$ResourceType with ID $ID not found."
            return
        }
        
        if (-not $resource) {
            Write-Warning "$ResourceType with ID $ID not found."
            return
        }
    }
    
    # Use the caller's ShouldProcess
    if ($CallerPSCmdlet.ShouldProcess("$ResourceType ID $ID ($resourceName)", $Action)) {
        Write-Verbose "$Action for $ResourceType ID ${ID}: $resourceName"
        
        try {
            $response = Invoke-CloudApiRequest -Uri $Uri -Method Patch -Body $Body
            
            if ($SuccessMessage) {
                Write-Host $SuccessMessage -ForegroundColor Green
            } else {
                Write-Host "$ResourceType $ID updated successfully." -ForegroundColor Green
            }
            
            return $response
        }
        catch {
            if ($_.Exception.Message -match "not found|does not exist") {
                Write-Warning "$ResourceType $ID not found."
                return
            }
            else {
                throw
            }
        }
    }
}