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 ERP
    The ERP to use.

    .Parameter LegacySite
    The site name to use. Only use if auto-detection does not work.

    .Parameter NoUninstall
    Skip legacy product uninstall step.

    #>

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

    $erpValid = Test-ERPName $ERP
    if (!$erpValid) {
        Write-Warning "$ERP is not a valid value"
        Write-ERPNames
        Write-Warning "Migration canceled"
        return
    }

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

    Write-Verbose "ERP is $ERP"
    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
    }

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

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

# Test prerequisites
    $iisCommand = Get-Command New-WebAppPool
    if ($iisCommand -eq $null) {
        Write-Warning "Command New-WebAppPool does not exist."
        Write-Warning "Please run Test-P1Prerequisites."
        return
    }

# Search role site
    Write-Section "Getting legacy web site information"

    $WebAppName = $roleInfo.WebApplicationName
    if ($LegacySite -eq "") {
        Write-Verbose "Searching for site containing web application $WebAppName"
        $installedWebApp = get-webapplication $WebAppName
        if ($installedWebApp -eq $null) {
            Write-Warning "Cannot find a web application named $WebAppName"
            Write-Warning "Relaunch with -LegacySite parameter"
            return
        }

        $searchSiteName = $installedWebApp.GetParentElement()["Name"]
        $webSite = Get-WebSite $searchSiteName
        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
    }

    $site = Get-WebSite $legacySiteName
    if ($site -eq $null) {
            Write-Warning "Site $legacySiteName does not exist."
            Write-Warning "Update canceled"
            return
    }

# Using first binding port as default port is site is uninstalled by MSI and must be recreated
    $portStr = $site.Bindings.Collection[0].bindingInformation
    $legacyPort = Get-BindingPort $portStr
    Write-Verbose "Legacy first binding port is $legacyPort"

    Write-OK "Legacy Web site found"

# Search for MSI uninstall from path and uninstall
    if ($NoUninstall) {
        Write-OK "Skipping legacy product uninstall step."
    } else {
        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
        if ($componentId -eq $null) {
            Write-Warning "Cannot find component Id for PlannerOne installed in $msiLocation"
            Write-Warning "Rerun with -NoUninstall if you want to by pass uninstallation step"
            Write-Warning "Update canceled"
            return
        }
        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 -ErrorAction "SilentlyContinue" | Out-Null
    Write-OK "Legacy PlannerOne uninstalled"

# Install Web app
    $webReport = New-P1WebApp -Tenant $Tenant -WebAppName $WebAppName -LegacySite $legacySiteName -Port $legacyPort -ERP $ERP
    $SitePort = $webReport.Port
    Write-OK "Web created on port $SitePort"

# Migrate role to tenant (register tenant, app server, web app)
    Write-Section "Migrating Role to Tenant"
    $RemotingPort = $roleInfo.RemotingPort
    $WebServicePort = $roleInfo.WebServicePort
    $adapter = $ERPToAdapter.Get_Item($ERP)
    Write-Verbose "Adapter associated to ERP is $adapter"
    Register-Tenant -Tenant $Tenant -RemotingPort $RemotingPort -WebServicePort $WebServicePort -WebAppName $WebAppName -SitePort $SitePort -Adapter $adapter
    $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
    if ($webReport.NeedSecuritySet -eq $true) {
        Write-Verbose "Setting default security to Windows authentication"
        Set-P1WebAuthentication $Tenant -Windows
    }

    Write-Section "Migrating configuration..."
    Migrate-Configuration $Role
    Write-OK "Configuration migrated"
# Install App Server
    New-P1ServerInstance -Tenant $Tenant -ERP $ERP
    Write-OK "Windows service created"

    Write-OK "Role $Role migrated"
    }
}