Public/Switch-P1Version.ps1

function Switch-P1Version {
    <#
    .Synopsis
    Switch to another version an existing PlannerOne tenant (service + web app).
 
    .Description
    Switch an existing PlannerOne tenant to another version.
    Warning: PlannerOne service will be restarted during the process.
 
    .Parameter Tenant
    The tenant name.
 
    .Parameter Version
    The version to switch to.
 
    .Example
    # Switch a PlannerOne tenant named P1Prod to V6.0.1624.73941.
    Switch-P1Tenant -Tenant P1Prod -Version 6.0.1624.73941
    #>

    [cmdletbinding()]
    param( 
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [string] $Version,
        [string] $WebAppName,
        [string] $LegacySite
    )
    Process
    {
        if ($Version -eq "") {
            $VersionStr = "LAST"
        } else {
            $VersionStr = $Version
        }

        Write-Section "Switch tenant $Tenant to version $VersionStr"

        if (!(Test-Tenant $Tenant)) {
            Write-Warning "Tenant $Tenant does not exist."
            Write-Warning "Type Get-P1Tenant for a list of tenants."
            return
        }

        $TenantInfo = Get-P1Tenant $Tenant
        $Adapter = $TenantInfo.Adapter

# Retro compatibility with legacy NAV tooling
        if ($Adapter -eq "") {
            $Adapter = "NAV"
        }

        $erp = $AdapterToERP.Get_Item($Adapter)

        $packageSrvName = Get-PackageName $P1SrvPackage $erp
        if ($Version -eq "") {
            $package = Get-Package $packageSrvName
        } else {
            $package = Get-Package -Name $packageSrvName -RequiredVersion $Version
        }

        if ($package -eq $null) {    
            Write-Warning "Package $packageSrvName does not exist for version: $VersionStr."
            Write-Warning "Call 'Get-Package $packageSrvName -AllVersion' to get a list of available package versions."
            Write-Warning "Call Install-P1Package $packageSrvName to get last package version."
            return
        }

        $packageWebName = Get-PackageName $P1WebPackage $erp
        if ($Version -eq "") {
            $package = Get-Package $packageWebName
        } else {
            $package = Get-Package -Name $packageWebName -RequiredVersion $Version
        }

        if ($package -eq $null) {    
            Write-Warning "Package $packageWebName does not exist for version: $VersionStr."
            Write-Warning "Call 'Get-Package $packageWebName -AllVersion' to get a list of available package versions."
            Write-Warning "Call Install-P1Package $packageWebName to get last package version."
            return
        }


        $serviceName = Get-ServiceNameFromTenant $Tenant
        $service = Get-Service -Name $serviceName
        if ($service -eq $null) {
                Write-Warning "Service $serviceName does not exist"
                Write-Warning "Switch operation canceled"
                return
        }
                
        if ($Version -eq "") {
            $srvBinPath = Get-PackageFolder $packageSrvName
        } else {
            $srvBinPath = Get-PackageFolderForVersion $packageSrvName $Version
        }

        Write-Verbose "Server package folder is $srvBinPath"
        $srvBinFolder = "AppServer"

        #Test if service already exists
        # Link folder to repository
        $serverPath = Get-ServerPathFromTenant $Tenant
        Write-Verbose "Server path is $serverPath"
        if (!(Test-Path $serverPath)) {
            Write-Warning "$serverPath does not exist."
            Write-Warning "Switch operation canceled."
            return
        }
        
        if ($LegacySite -eq "") {
            $siteName = Get-SiteNameFromTenant $Tenant
        } else {
            Write-Verbose "Site name forced to $LegacySite"
            $siteName = $LegacySite
        }

        if (!$WebAppName) {
            $WebAppName = Get-WebAppNameFromTenant $Tenant
        }

        $sitePath = "$IISRootPath\$siteName"
        $webAppPath = "$sitePath\$WebAppName"
        Write-Verbose "Web app path is $webAppPath"
                
        if ($Version -eq "") {
            $webBinPath = Get-PackageFolder $packageWebName
        } else {
            $WebBinPath = Get-PackageFolderForVersion $packageWebName $Version
        }

        Write-Verbose "Web package folder is $webBinPath"

        if (!(Test-Path $webAppPath)) {
            Write-Warning "$webAppPath does not exist."
            Write-Warning "Switch operation canceled."
            return
        }
        
        Write-Output "Switching server..."
        Stop-Service $serviceName
        Remove-Symlink $serverPath
        New-Symlink $serverPath "$srvBinPath\$srvBinFolder"
        Start-Service $serviceName

        Write-Output "Switching web application..."
        Remove-Symlink $webAppPath
        New-Symlink $webAppPath "$webBinPath\WebServer"
        Restore-P1WebSecurity $Tenant
        Start-Service $serviceName

        Write-OK "Tenant '$Tenant' switched"
    }
}