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 (Test-Path $Folder -PathType Container) {
        $ContainerName = Get-NewContainerName -ContainerName $ContainerName

        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)
            }
        }

        $tempVersion = Get-AppKeyValue -KeyName 'platform'
        if ($null -eq $tempVersion -or $tempVersion -eq "") {
            $tempVersion = Get-BcContainerNavVersion -containerOrImageName $ContainerName
            [version]$platform = [version]$tempVersion.Split('-')[0]
        }
        else {
            [version]$platform = $tempVersion
        }
        if (!(Get-UseStandardTest -MajorVersion $platform.Major) -and ($platform.Major -gt 10)) {
            Enable-SymbolLoading -ContainerName $ContainerName
            Start-Sleep -Seconds 5
        }

        $files = Get-ChildItem -Path $folder -include ('*.TXT', '*.FOB') -Recurse

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

            Compile-ObjectsInNavContainer -containerName $ContainerName -SynchronizeSchemaChanges Force -sqlCredential $Credential
        }
        catch {
            exit 1
        }
        Write-Output "All objects imported and compiled"
    } else {
        Write-Output "No Objects found"
    }

    exit 0
}
Export-ModuleMember Import-ObjectsFromFolder