# ===================================================================================
# Upgrade-CopyMove.ps1
# (c) SharePoint Products ApS, 2010-2026
#
# CopyMove upgrade script
# ===================================================================================

$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
{
	$Host.UI.RawUI.BackgroundColor = "DarkBlue"
	clear-host
} else
{
    $arguments = $myInvocation.MyCommand.Definition;
    Start-Process powershell.exe -Verb RunAs -ArgumentList "-file `"$arguments`""; 
    exit;
}

$Host.UI.RawUI.WindowTitle = "Upgrading CopyMove for SharePoint Server Subscription Edition"
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
. "$dp0\Functions.ps1"

Load-SharePoint-Powershell;

Write-Host "Checking current version ..." -NoNewline;
$upgradeMinimumVersion = New-Object System.Version("6.0.0.0");
$installedVersion = Get-InstalledVersion;

if ($installedVersion -eq $null)
{
	Write-Host " CopyMove is not installed!";
} elseif ($installedVersion -lt $upgradeMinimumVersion)
{
	Write-Host " $installedVersion";
    Write-Warning "The currently installed version cannot be upgraded! Please reinstall CopyMove by first uninstalling the old version using the Uninstall-CopyMove.cmd script. Then install the new version using the Install-CopyMove.cmd script. The reinstall is safe and will not drop any settings.";
} else
{
	Write-Host " $installedVersion";
    Write-Host "Upgrading CopyMove ...";

	Write-Host "Disabling CopyMove farm feature ..." -NoNewline;
	$feature = Get-SPFeature -Identity SPPCopyMoveFarm -Farm -ea SilentlyContinue;
	if ($feature -ne $null)
	{
		start -NoNewWindow -Wait powershell {
			$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue;
			Disable-SPFeature -Identity SPPCopyMoveFarm -Confirm:$false
		}
		Write-Host -f Green " success";
	} else
	{
		Write-Host -f Yellow " skipped - feature not active";
	}

	Uninstall-PowerShellModule "SharePointProducts.CopyMove.PowerShell";
	Uninstall-PowerShellModule "SharePointProducts.Platform.PowerShell";

    Upgrade-FarmSolution "sharepointproducts_platform.wsp" "$dp0\..\sharepointproducts_platform.wsp";
    Upgrade-FarmSolution "sharepointproducts_copymove.wsp" "$dp0\..\sharepointproducts_copymove.wsp";

	Install-PowerShellModule "$dp0\.." "SharePointProducts.Platform.PowerShell";
	Install-PowerShellModule "$dp0\.." "SharePointProducts.CopyMove.PowerShell";

    $ServiceNames=@("SPTimerV4")
    Restart-SharePointServicesOnAllServers $ServiceNames;

	Write-Host "Enabling CopyMove farm feature ..." -NoNewline;
	start -NoNewWindow -Wait powershell {
		$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue;
		Enable-SPFeature -Identity SPPCopyMoveFarm;
	}
    Write-Host -f Green " success";

	Write-Host ""
	Write-Host "Upgrade complete - configure CopyMove as follows:"
	Write-Host "1. Navigate to 'Central Administation -> Manage farm features'"
	Write-Host "2. Ensure that the feature 'CopyMove for SharePoint Server SE' is activated"
	Write-Host "3. Activate the CopyMove web application feature on all applicable web applications"
	Write-Host "4. Activate the CopyMove site collection feature on all applicable site collections"
	Write-Host "5. Optionally change default settings from 'Central Administration -> CopyMove settings'"
	Write-Host "6. Done"
	Write-Host ""
}

Pause

