Public/Export-P1LegacyEnvironment.ps1

function Export-P1LegacyEnvironment {
    <#
    .Synopsis
    Export legacy PlannerOne environment to file.
 
    .Description
    Export an existing and initialized legacy environment stored in a database and save it to file system.
    Each environments found for Production Scheduler and ResourcePlanner will be exported in its own file in -OutDir directory.
 
    .Parameter SQLServer
    The SQL Server host name and instance.
 
    .Parameter Database
    The SQL Server database name.
 
    .Parameter OutDir
    The root directory path to use for export. .\p1EnvExport by default.
 
    .Parameter ForceDatatableName
    Force datatable name to export. By default script will export sequentially dbo.EnvironmentProductionDao and dbo.EnvironmentProjectDao.
 
    .Parameter Product
    Select product to export environment from.
    Valid values are: Production, Projects, All.
    Default value is All.
    All option cannot be used with ForceDatatableName.
 
    .Parameter UseUIdAsName
    Do not try to retrieve Planning name and Planning Group Container to create directories.
    Use environment UId.
    Use this option if environments names are not valid for directory names.
     
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string] $SQLServer,
        [Parameter(Mandatory=$true)]
        [string] $Database,
        [string] $OutDir,
        [string] $ForceDatatableName,
        [string] $Product,
        [switch] $UseUIdAsName
    )    
    Process {
        if ($Product -eq "") {
                $Product = "All"
        }

        if ($Product -ne "Production" -and $Product -ne "Project" -and $Product -ne "All") {
                Write-Warning "Valid values for -Product are: Production, Project, All"
                Write-Warning "Operation canceled."
                return
        }

        $currentDir = (Get-Item -Path ".\").FullName
        if ($OutDir -eq "") {            
            $OutDir = $currentDir + "\p1EnvExport"
        } else {
            $rooted = [System.IO.Path]::IsPathRooted($OutDir)
            if ($rooted -eq $false) {
                $OutDir = $currentDir + "\" + $OutDir
            }
        }

        Write-Verbose "OutDir is $OutDir"

        $connectionString = Get-ConnectionString -Server $SQLServer -Database $Database -IntegratedSecurity "true"

        if ($ForceDatatableName -eq "") {
            if ($Product -eq "Production" -or $Product -eq "All") {
                $PSDir = $OutDir + "\Production"             
                Write-Section "Production Scheduler environments"
                Export-DBEnvironments -connectionString $connectionString -dataTable "dbo.EnvironmentProductionDao" -OutDir $PSDir -useUId:$UseUIdAsName
            }

            if ($Product -eq "Project" -or $Product -eq "All") {                    
                $RPDir = $OutDir + "\Project"
                Write-Section "Resource Planner environments"
                Export-DBEnvironments -connectionString $connectionString -dataTable "dbo.EnvironmentProjectDao" -OutDir $RPDir -useUId:$UseUIdAsName
            }
        } else {
            if ($Product -eq "All") {
                    Write-Warning "Product cannot be All with ForceDatatableName option"
                    Write-Warning "Operation canceled."
                    return;
            }

            Write-Verbose "Reading $ForceDatatableName environments..."
            Export-DBEnvironments -connectionString $connectionString -dataTable $ForceDatatableName -OutDir $OutDir -useUId:$UseUIdAsName
        }
    }
}