Public/Update-P1LegacyRole.ps1

function Update-P1LegacyRole {
    <#
    .Synopsis
    Migrate an installed legacy role to new tenant and WMF system.
 
    .Description
    Search for existing Role and existing MSI installation to migrate to new tenant and WMF install system.
 
    .Parameter Role
    The role name to migrate.
     
    .Parameter LegacySite
    The site name to use. Only use if auto-detection does not work.
     
    #>

    [cmdletbinding()]
    param(
    [Parameter(Mandatory=$true)]
    [string] $Role,
    [string] $LegacySite
    )    
    Process {
# Read role file for $Role and capture informations
        $roleInfo = Get-P1LegacyRole $Role
    if (!$roleInfo) {
            Write-Warning "Role $Role does exist"
        return
    }

    Write-Section "Migrating legacy Role of PlannerOne..."
    $Tenant = $Role

    if (Test-Tenant $Tenant) {
        Write-Warning "Tenant $Tenant already exist. Migration canceled."
        return
    }

# Test PlannerOne Manager is not running
    $rolePath = $roleInfo.Path
    $PM = Get-Process | Where-Object { $_.Path -eq "$rolePath\PlannerOneManager.exe" }
    if ($null -ne $PM) {
        Write-Warning "Please close PlannerOne Manager for the role $Role before migration."
        Write-Warning "Migration canceled"
        return
    }

# Test Database access
    $dbConfig = Test-DBConfig $Role
    if ($dbConfig -eq $false) {
        Write-Warning "Migration canceled."
        return
    }
    
    $dbConnect = Test-DBConnection $Role
    if ($dbConnect -eq $false) {
        Write-Warning "Migration canceled."
        return
    }   

# Search role site
    Write-Section "Getting legacy web site information"
        $siteName = Get-SiteNameFromTenant $Tenant
    $WebAppName = $roleInfo.WebApplicationName
    if ($LegacySite -eq "") {
        Write-Verbose "Searching for site containing web application $WebAppName"
            $webSite = Get-WebSite((get-webapplication $WebAppName).GetParentElement()["Name"])
        if ($webSite.Count -gt 1) {
            Write-Warning "Cannot determine which web site to use: Multiple web application with name $WebAppName"
            Write-Warning "Relaunch with -LegacySite parameter"
            return
        }

        $legacySiteName = $webSite.Name 
    } else {
        $legacySiteName = $LegacySite
    }
    
    Write-OK "Legacy Web site found"

# Search for MSI uninstall from path and uninstall
    Write-Section "Uninstalling existing legacy product (can take several minutes...)"
    $rolePath = $roleInfo.Path
    $msiLocation = (get-item $rolePath).parent.FullName + "\"
    Write-Verbose "Searching for PlannerOne uninstall information for location $msiLocation"
    $uninstallInfo = Get-P1MsiInstall -OnlyServer -InstallLocation $msiLocation
    $componentId = $uninstallInfo.PSChildName
    Write-Verbose "Component ID to remove is $componentId"
    Write-Verbose "Uninstalling product"

        Uninstall-P1Msi -UninstallKey $componentId
# Remove service and web app in case of uninstall error
    Write-Verbose "Removing legacy windows service"
    Remove-Service     $roleInfo.WindowsServiceName | Out-Null
    Write-Verbose "Removing legacy web app"
    Remove-WebApplication -Name $WebAppName -Site $legacySiteName | Out-Null
    Write-OK "Legacy PlannerOne uninstalled"

# Migrate role to tenant (register tenant, app server, web app)
        Write-Section "Migrating Role to Tenant"
    $RemotingPort = $roleInfo.RemotingPort
    $WebServicePort = $roleInfo.WebServicePort
        Register-Tenant -Tenant $Tenant -RemotingPort $RemotingPort -WebServicePort $WebServicePort -WebAppName $WebAppName
    $migrationReport = @{
        MigrationDone = $true;
        MigrationSuccess = $true;
        MigrationError = ""
    }

    $json = $migrationReport | ConvertTo-Json
    $storeBasePath = Get-GlobalReportPath
    $storePath = $storeBasePath + $Tenant + ".json"
    if (!(Test-Path $storeBasePath)) {
        mkdir $storeBasePath | Out-Null
    }

    $json | Out-File $storePath
    Write-OK "Tenant created"
# Configuration migration
    Write-Section "Migrating configuration..."
    Migrate-Configuration $Role
    Write-OK "Configuration migrated"
# Install App Server
        New-P1ServerInstance -Tenant $Tenant

# Install Web app
        New-P1WebApp -Tenant $Tenant -WebAppName $WebAppName -LegacySite $legacySiteName

    Write-OK "Role $Role migrated"
    }
}