Public/Switch-P1DEVTenant.ps1

function Switch-P1DEVTenant {
    <#
    .Synopsis
    Switch to another DEV branch an existing PlannerOne tenant (service + web app).
     
    .Description
    Switch an existing PlannerOne tenant to another branch.
    Warning: PlannerOne service will be restarted during the process.
     
    .Parameter Tenant
    The tenant name.
     
    .Parameter DevType
    The type of the tenant could be ApAdapter, NavAdapter or PlanningManagement.
     
    .Parameter Branch
    The branch to switch to (trunk if empty).
     
    .Example
    # Switch a PlannerOne tenant named DEVPM to branch PO-1234.
    Switch-P1DEVTenant -Tenant DEVPM -Branch PO-1234
    #>

    [cmdletbinding()]
    param(
    [Parameter(Mandatory=$true)] 
    [string] $Tenant, 
    [Parameter(Mandatory=$true)] 
    [ValidateSet('ApAdapter','NavAdapter','PlanningManagement')] 
    [string] $DevType, 
    [string] $Branch
    )
    Process
    {
        
        Write-Output "Preparing to switch tenant $Tenant ..."
        
        if ($Branch) {
            $webBinPath = "C:\dotnet\projects\PlannerOne\branches\$Branch\PlannerOneWebASP\PlannerOneWebASP"
            $srvBinPath = "C:\dotnet\projects\PlannerOne\branches\$Branch\$DevType\bin\Debug"
        } else { 
            $webBinPath = "C:\dotnet\projects\PlannerOne\trunk\PlannerOneWebASP\PlannerOneWebASP"
            $srvBinPath = "C:\dotnet\projects\PlannerOne\trunk\$DevType\bin\Debug"
        }

        Write-Output ">> webBinPath: $webBinPath"
        Write-Output ">> srvBinPath: $srvBinPath"
        
        $TenantInfo = Get-P1Tenant $Tenant
        $serverPath = $TenantInfo.Path
        $siteName = (Find-WebSite $TenantInfo.SitePort).Name
        
        $webApp = Get-P1WebApplication $Tenant
        $webAppPath = $webApp.PhysicalPath

        Write-Output " "        
        Write-Output ">> current webAppPath: $webAppPath"
        Write-Output ">> current serverPath: $serverPath"
        
        $webAppName = $TenantInfo.WebApplicationName
        $poolName = $webApp.applicationPool
        $StorePath = Get-LocalStorePathFromTenant $Tenant

        $LinksStorePath = $StorePath + 'Bin'
        if (!(Test-Path $LinksStorePath) ) {
            New-Item -ItemType Directory -Force -Path $LinksStorePath
        }

        Write-Output "Stopping web application $poolName"
        Stop-WebAppPool "$poolName"
        
        Write-Output "Switching server..."
        if (Test-Symlink($serverPath)) {
            Remove-Symlink $serverPath
        }
        
        # $srvLinkPath = $LinksStorePath + "\AppServer"
        # $TenantInfo.Path = $srvLinkPath
        # New-Symlink $srvLinkPath "$srvBinPath"
        $TenantInfo.Path = $srvBinPath
        Write-TenantInfo $Tenant $TenantInfo 


        Write-Output "Switching web application..."
        if (Test-Symlink($webAppPath)) {
            Remove-Symlink $webAppPath
        }

        # $webLinkPath = $LinksStorePath + "\WebServer"
        # New-Symlink $webLinkPath "$webBinPath" -Force
        Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='$siteName']/application[@path='/$webAppName']/virtualDirectory[@path='/']" -name 'PhysicalPath' -value $webBinPath

        Restore-P1WebSecurity $Tenant
        
        Write-Output "Starting web application $poolName"
        Start-WebAppPool "$poolName"
        
        Write-OK "Tenant '$Tenant' switched"
    }
}