Private/Container/Initialize-NAVGitContainerCommand.ps1

function Initialize-NAVGitContainerCommand {
    <#
    .SYNOPSIS
        Common start of commands that work on an existing container: loads the configuration, checks elevation and
        BcContainerHelper, checks that the container exists and gets the credentials.
        Returns [pscustomobject]@{ Config; ContainerName; Credential; SqlCredential }.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string] $Path,

        [Parameter(Mandatory)]
        [string] $Operation
    )

    $config = Get-NAVGitConfig -Path $Path
    Assert-NAVGitAdministrator -Operation $Operation
    Assert-NAVGitBcContainerHelper -UserSettings $config.userSettings

    $containerName = $config.containerName
    if (-not (Test-BcContainer -containerName $containerName)) {
        throw "Container '$containerName' does not exist. Build it first (New-NAVGitContainer)."
    }

    $auth = $null
    $state = Get-NAVGitContainerState -ContainerName $containerName
    if ($state -and $state.auth) {
        $auth = $state.auth
    }
    else {
        $auth = Get-NAVGitEffectiveAuth -Auth $config.auth
    }

    $credential = Get-NAVGitCredential -ContainerName $containerName -Auth $auth
    return [pscustomobject]@{
        Config        = $config
        ContainerName = $containerName
        Credential    = $credential
        SqlCredential = Get-NAVGitSqlCredential -Auth $auth -Credential $credential
    }
}

function Get-NAVGitSqlCredential {
    <#
    .SYNOPSIS
        SQL credential for finsql operations: for NavUserPassword containers BcContainerHelper sets the 'sa' password
        to the container password; Windows containers use integrated security ($null).
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string] $Auth,

        [pscredential] $Credential
    )

    if ($Auth -eq 'Windows' -or -not $Credential) {
        return $null
    }
    return New-Object System.Management.Automation.PSCredential('sa', $Credential.Password)
}

function Get-NAVGitStagingFolder {
    <#
    .SYNOPSIS
        Creates a new, empty folder that is shared with the container:
        <hostHelperFolder>\Extensions\<container>\navgit\<guid>. Callers remove it when done.
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory)]
        [string] $ContainerName
    )

    $helperConfig = Get-Variable -Name bcContainerHelperConfig -Scope Global -ValueOnly -ErrorAction SilentlyContinue
    $hostHelperFolder = Join-Path $env:ProgramData 'BcContainerHelper'
    if ($helperConfig -and $helperConfig.hostHelperFolder) {
        $hostHelperFolder = $helperConfig.hostHelperFolder
    }

    $folder = Join-Path $hostHelperFolder "Extensions\$ContainerName\navgit\$([guid]::NewGuid().ToString('N'))"
    New-Item -ItemType Directory -Path $folder -Force | Out-Null
    return $folder
}

function Invoke-NAVGitContainerCompile {
    <#
    .SYNOPSIS
        Compiles objects in the container and reports objects that are still uncompiled.
        Symbols are generated by BcContainerHelper when symbol loading is enabled on the server.
        Returns the uncompiled objects.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        $Context,

        [string] $Filter = $Context.Config.import.compileFilter
    )

    $what = 'all objects'
    if ($Filter) { $what = "objects ($Filter)" }
    Write-NAVGitLog "Compiling $what" -Level Step
    try {
        Compile-ObjectsInNavContainer -containerName $Context.ContainerName -filter $Filter `
            -sqlCredential $Context.SqlCredential -SynchronizeSchemaChanges $Context.Config.import.syncSchema
    }
    catch {
        # Compile errors are reported below from the Compiled flag; other failures still surface here.
        Write-NAVGitLog "Compile reported errors: $($_.Exception.Message)" -Level Warning
    }

    $uncompiled = @(Get-NAVGitUncompiledObjects -ContainerName $Context.ContainerName)
    if ($uncompiled.Count -gt 0) {
        $list = ($uncompiled | Select-Object -First 25 | ForEach-Object { " $($_.Type) $($_.Id) $($_.Name)" }) -join [Environment]::NewLine
        $more = ''
        if ($uncompiled.Count -gt 25) { $more = "$([Environment]::NewLine) ... and $($uncompiled.Count - 25) more" }
        Write-NAVGitLog "$($uncompiled.Count) object(s) are not compiled:$([Environment]::NewLine)$list$more" -Level Warning
    }
    else {
        Write-NAVGitLog 'All objects are compiled' -Level Success
    }
    return $uncompiled
}

function Invoke-NAVGitForceSync {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string] $ContainerName
    )

    Write-NAVGitLog 'Synchronizing schema (ForceSync)' -Level Step
    Invoke-ScriptInBcContainer -containerName $ContainerName -scriptblock {
        Get-NAVTenant -ServerInstance $ServerInstance | Sync-NAVTenant -Mode ForceSync -Force
    }
}