ObjectHandling/Import-ObjectsFromFolder.ps1

<#
.synopsis
    Imports objects from a folder into a container
.description
    Imports objects from a folder into a container and compiles them. If NAV 2018, enable symbol loading will be turned on
.parameter ContainerName
    Container to be used. Can be provided in the settings.json
.parameter Folder
    Folder the objects should be imported from
.parameter Credential
    SQL Credentials to import objects
.example
    Import-ObjectsFromFolder -ContainerName test -Folder C:\temp -Credential (Get-Credential)
#>

function Import-ObjectsFromFolder {
    Param(
        [Parameter(Mandatory=$false)]
        [string] $ContainerName,
        [Parameter(Mandatory=$true)]
        [string] $Folder,
        [Parameter(Mandatory=$false)]
        [pscredential] $Credential
    )

    if ($null -eq $ContainerName -or $ContainerName -eq "") {
        $ContainerName = (Get-EnvironmentKeyValue -KeyName 'name')
    }

    if ($null -eq $Credential) {
        $NewCredential = Get-CredentialFromEnvironmentJson
        if ($NewCredential -eq $false) {
            $Password = (ConvertTo-SecureString "Password" -AsPlainText -Force)
            $Credential = [PSCredential]::new('sa', $Password)
        }
        else {
            $Credential = [PSCredential]::new('sa', $NewCredential.Password)
        }
    }

    [version]$platform = (docker inspect $ContainerName | ConvertFrom-Json).Config.Labels.version
    if ($platform.Major -eq 11) {
        Enable-SymbolLoading -ContainerName $ContainerName
        Start-Sleep -Seconds 5
    }

    if (Test-Path $Folder -PathType Container) {
        $files = Get-ChildItem -Path $folder -include ('*.TXT', '*.FOB') -Recurse

        $files | ForEach-Object {
            Import-ObjectsToNavContainer -containerName $ContainerName -objectsFile $_.FullName -sqlCredential $Credential -ImportAction Overwrite -SynchronizeSchemaChanges Force
        }

        Compile-ObjectsInNavContainer -containerName $ContainerName -SynchronizeSchemaChanges Force -sqlCredential $Credential
    } else {
        Write-Output "No Objects found"
    }
}
Export-ModuleMember Import-ObjectsFromFolder