Public/Install-P1Package.ps1

function Install-P1Package {
    <#
    .Synopsis
    Get all required packages from PlannerOne NuGet server.
 
    .Description
    Connect to remote repository and get the latest release.
     
    .Parameter Package
    The PlannerOne package list.
 
    .Parameter ERP
    The target ERP to get default packages for if not provided in Package list (NAV, AP, SAGE)
 
    .Parameter Prerelease
    Allow prerelease versions to be installed
 
    .Example
    # Install PlannerOne server, web and NAV 2016 objects
    Install-P1Package PlannerOne.Server,PlannerOne.WebApp,PlannerOne.NAV2016.Objects
 
    .Example
    # Install PlannerOne SAGE server and web
    Install-P1Package -ERP SAGE
    #>

    [cmdletbinding()]
    param( 
        [Parameter(Mandatory=$False)] [string[]] $Package,
        [Parameter(Mandatory=$False)] [string]   $ERP, 
        [Parameter(Mandatory=$False)] [string]   $Version='' 
    )    
    Process {
        $feedUrl = Get-FeedUrl
        if ($feedUrl -eq "") {
            return
        } else {
            try {
                $plannerOneSource = Get-PackageSource PlannerOne -ErrorAction "SilentlyContinue"
            }
            catch {}

            if ($plannerOneSource -eq $null) {
                Write-Verbose "Registering $feedUrl as a trusted package source for PlannerOne."
                Register-PackageSource -Name PlannerOne -Trusted -ProviderName NuGet -Location $feedUrl | Out-Null
            }
        }

        if ($null -eq $Package) {
            if ($ERP -eq "") {
                Write-Warning "No package specified and no ERP Specified. Cannot continue."
                return
            }

            $packageSrvWeb = Get-PackageName $P1SrvPackage $ERP
            $packageWebName = Get-PackageName $P1WebPackage $ERP            
            
            $Package = @(
                        $packageSrvWeb;
                        $packageWebName;                            
                        )
                        
            if($ERP.ToUpper() -eq "NAV"){
                if($Version -eq ""){
                    Write-Warning "If you use NAV adapter, you need to specify its version."
                    Write-Warning "If you do not want to install NAV package run Install-P1Package PlannerOne.NAV.Server,PlannerOne.NAV.WebApp"
                    Write-Warning "Cannot continue."
                    return 
                }elseif(!(Test-NavVersionCompatibility $Version)){
                    Write-Warning "The NAV version $Version is not compatible with this script."
                    Write-Warning "If you do not want to install NAV package run Install-P1Package PlannerOne.NAV.Server,PlannerOne.NAV.WebApp"                    
                    Write-Warning "The compatible version are $($NavVersion -join ", "). Cannot continue."
                    return 
                }else{
                    $Package += Get-Version $P1NavToolPackage $Version
                }
            }
        }
        
        Write-Section "Getting PlannerOne packages ($($Package -join ", "))..."

        if (Get-Command Install-Package -errorAction SilentlyContinue)
        {
            Write-Verbose "Install-Package exists"

            foreach($Pack in $Package) {
                Write-Output "Installing $Pack..."
                Install-Package $Pack -Source $feedUrl -AllowPrereleaseVersions:$Prerelease.IsPresent | Out-Null
                $Folder = Get-PackageFolder $Pack
                Write-OK "Package installed in $Folder"
            }
        } else {
            Write-Output "Install-Package doesn't exist, using NuGet"

            foreach($Pack in $Package) {
                Write-Output "Installing $Pack..."
                NuGet install $Pack -Source $feedUrl
            }
        }

        Write-OK "Packages installed"

        foreach($Pack in $Package){
            if($Pack -match 'PlannerOne.NAV.(\d{4})') {
                $year = $Pack.replace("PlannerOne.NAV.","")

                Import-P1NavTool $year
            }
        }

    }
}