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
    )

    $lockPath = Join-Path $ChildRoot (Join-Path '.kaden' (Join-Path 'transactions' 'update.lock'))
    $lockHeld = $false
    try {
        Invoke-KadenManagedInitialize -ChildRoot $ChildRoot -ProjectId $ProjectId `
            -Manifest $Manifest -PayloadRoot $PayloadRoot -ModuleVersion $ModuleVersion -KeepLock
        $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
        }
    }
}