ObjectHandling/Export-NavObjectsForMerge.ps1

<#
.synopsis
    Exports objects from a NAV database as basis for an object merge
.description
    Exports objects from a NAV database based on a local repository
.parameter ContainerName
    Container the objects are exported from. Can be read from settings.json
.parameter SourceFolder
    Repository Folder with the product to be merged
.parameter DestinationFolder
    Folder the exported objects should be saved at
.parameter Credential
    SQL credentials to export the objects
.example
    Export-NavObjectsForMerge -SourceFolder "D:\source" -DestinationFolder "D:\target"
#>

function Export-NavObjectsForMerge {
    Param(
        [Parameter(Mandatory=$false)]
        [string] $ContainerName,
        [Parameter(Mandatory=$true)]
        [string] $SourceFolder,
        [Parameter(Mandatory=$true)]
        [string] $DestinationFolder,
        [Parameter(Mandatory=$false)]
        [pscredential] $Credential
    )

    $ContainerName = Get-NewContainerName -ContainerName $ContainerName

    if ($null -eq $Credential) {
        $NewCredential = Get-CredentialFromEnvironmentJson
        if ($NewCredential -eq $false) {
            $Credential = (Get-Credential)
        }
        else {
            $Credential = $NewCredential
        }
    }

    if (!(Test-Path $DestinationFolder -PathType Container)) {
        New-Item $DestinationFolder -ItemType Directory -Force | Out-Null
    }

    $containerFolder = ((Get-BcContainerSharedFolders $ContainerName).GetEnumerator() | Where-Object { $_.Value -eq "C:\Run\my"}).Name
    $files = Get-ChildItem -Path $SourceFolder -Filter '*.TXT'

    $files | ForEach-Object {
        $type = $_.Name.Substring(0, 3).ToUpper()
        $id = $_.Name.Substring(3, $_.Name.Length - 7)

        switch ($type)
        {
            "TAB" { $objecttype = 1 }
            "PAG" { $objecttype = 8 }
            "REP" { $objecttype = 3 }
            "COD" { $objecttype = 5 }
            "QUE" { $objecttype = 9 }
            "XML" { $objecttype = 6 }
            "MEN" { $objecttype = 7 }
        }

        Export-NavContainerObjects -containerName $ContainerName -objectsFolder $containerFolder -filter "id=$id;type=$objecttype" -sqlCredential $Credential -exportTo 'txt file' -PreserveFormatting
        if (Test-Path (Join-Path $containerFolder "objects.txt") -PathType Leaf) {
            if ((Get-Item -Path (Join-Path $containerFolder "objects.txt")).Length -eq 0) {
                Copy-Item -Path $_.FullName -Destination (Join-Path $DestinationFolder $_.Name) -Force
            }
            else {
                Copy-Item -Path (Join-Path $containerFolder "objects.txt") -Destination (Join-Path $DestinationFolder $_.Name) -Force
            }
            Remove-Item (Join-Path $containerFolder "objects.txt") -Force
        }
    }
}
Export-ModuleMember Export-NavObjectsForMerge