Public/Switch-P1Tenant.ps1

function Switch-P1Tenant {
    <#
    .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
    {
        Write-Section "Switch tenant $Tenant to version $Version"
        Write-Warning "Migrate to LAST version only for now"

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

        $package = Get-Package $P1SrvPackage
        if ($package -eq $null) {    
            Write-Warning "Package $P1SrvPackage does not exist."
            Write-Warning "Call Install-P1Package with missing packages."
            return
        }

        $package = Get-Package $P1WebPackage
        if ($package -eq $null) {    
            Write-Warning "Package $P1WebPackage does not exist."
            Write-Warning "Call Install-P1Package with missing packages."
            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
        }
                
        $srvBinPath = Get-PackageFolder($P1SrvPackage)
        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"
                
        $webBinPath = Get-PackageFolder($P1WebPackage)
        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"
        Start-Service $serviceName

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