Private/Invoke-KadenManagedInstructionUpdate.ps1

function Invoke-KadenManagedInstructionUpdate {
    <#
    .SYNOPSIS
        Apply the managed payload and the delimited instruction write under one update lock.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)][string]$ChildRoot,
        [Parameter(Mandatory = $true)][string]$ProjectId,
        [Parameter(Mandatory = $true)]$Manifest,
        [Parameter(Mandatory = $true)][string]$PayloadRoot,
        [Parameter(Mandatory = $true)][string]$ModuleVersion,
        [Parameter(Mandatory = $false)][switch]$CreateMissingAgents,
        [Parameter(Mandatory = $false)][switch]$AssumeLock,
        [Parameter(Mandatory = $false)][string[]]$SkipDestinations = @()
    )

    $lockPath = Join-Path $ChildRoot (Join-Path '.kaden' (Join-Path 'transactions' 'update.lock'))
    $lockHeld = $false
    try {
        if ($AssumeLock) {
            Invoke-KadenManagedInitialize -ChildRoot $ChildRoot -ProjectId $ProjectId `
                -Manifest $Manifest -PayloadRoot $PayloadRoot -ModuleVersion $ModuleVersion `
                -AssumeLock -KeepLock -SkipDestinations $SkipDestinations
        }
        else {
            Invoke-KadenManagedInitialize -ChildRoot $ChildRoot -ProjectId $ProjectId `
                -Manifest $Manifest -PayloadRoot $PayloadRoot -ModuleVersion $ModuleVersion -KeepLock `
                -SkipDestinations $SkipDestinations
            $lockHeld = $true
        }
        if ($CreateMissingAgents) {
            return @(Update-KadenInstructionBlock -ChildRoot $ChildRoot -ProjectId $ProjectId -CreateMissingAgents -AssumeLock)
        }
        return @(Update-KadenInstructionBlock -ChildRoot $ChildRoot -ProjectId $ProjectId -AssumeLock)
    }
    finally {
        if ($lockHeld) {
            Remove-Item -LiteralPath $lockPath -Force -ErrorAction SilentlyContinue
        }
    }
}