ObjectHandling/Export-ObjectsToFolder.ps1

<#
.synopsis
    Export objects from a container to a folder
.description
    Export objects from a container to a folder based on filter credentials stored in the settings.json
.parameter ContainerName
    Container to be used. Can be provided in the settings.json
.parameter Folder
    Folder the objects should be exported to
.parameter Credential
    SQL Credentials to export objects
.example
    Export-ObjectsToFolder -ContainerName test -Folder C:\temp -Credential (Get-Credential)
#>

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

    $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('admin', $Password)
        }
        else {
            $Credential = $NewCredential
        }
    }

    if (!(Test-Path $Folder -PathType Container)) {
        New-Item $Folder -ItemType Directory -Force
    }

    $containerFolder = ((Get-BcContainerSharedFolders $ContainerName).GetEnumerator() | Where-Object { $_.Value -eq "C:\Run\my"}).Name
    $objectsFilter = (Get-EnvironmentKeyValue -KeyName 'objectsFilter')
    if (($null -eq $objectsFilter) -or ($objectsFilter -eq '')) {
        return
    }
    $objectsName = ("{0}_{1}_{2}_BaseObjects.fob" -f (Get-AppKeyValue -KeyName 'publisher'), (Get-AppKeyValue -KeyName 'name'), (Get-AppKeyValue -KeyName 'version'))

    Export-NavContainerObjects -containerName $ContainerName -objectsFolder "$containerFolder" -filter "$objectsFilter" -sqlCredential $Credential -exportTo 'fob file'

    if (Test-Path (Join-Path $containerFolder "objects.fob") -PathType Leaf) {
        Copy-Item -Path (Join-Path $containerFolder "objects.fob") -Destination (Join-Path $Folder $objectsName) -Force
        Remove-Item (Join-Path $containerFolder "objects.fob") -Force
    }
}
Export-ModuleMember Export-ObjectsToFolder