Sitecore 9.3 Uninstall Script

With Sitecore 9.2, developers where once again re-warded with a GUI interface to installation called the Sitecore Install Assistant or SIA. With the arrival of 9.3, the SIA has gotten some nice upgrades, such as Solr install and SXA installation. (Now if they could bake Habitat Home in, it would be the perfect demo setup tool ever.) As with the original, there is not an uninstall mechanism included. Uninstallation results in having to recall on the site details, and then entering them into the XPO_SingleDeveloper.ps1, and running.

In my opinion that’s a lot work to wipe out a version I no longer need or corrupted with some experiment gone bad.

For developers or admins who are a bit less comfortable with JSON and PowerShell the SIA does a great job prompting for the bare minimum for a successful installation. Instead of spending 20 minutes double checking configuration files, you can be up and running rather quickly via SIA. (Now including even, a Solr and SXA install options.)

What I am more interested in (and you are given you’ve ended up here reading) is how to perform an uninstall after using the Sitecore Install Assistant. (A future article will contain some notes and thoughts around SIA installation.)

The Script

Back in September I provided a script that helps to uninstall a Sitecore 9.2 installation that was done by the SIA, https://thecodeattic.wordpress.com/2019/09/17/uninstall-after-using-sitecore-install-assistant/. The code itself can be viewed on GitHub as well.

Using this as a starting point I have made some updates to simplify what you need to enter at the start, as well as taken into consideration changes and updates that have been made to the Sitecore Install Assistant logging with the release of 9.3.

In general, the script Sitecore-Uninstall-SIA.ps1 parses a SIA log file. New with this version is the ability to either specifically reference a log file or allow it to grab the lastest modified log file from the default log location, which is normally C:\Users\user.name\Sitecore.InstallAssistant.

If you made no changes to the default Sitecore Install Assistant configuration file, then all need to do is download the script and run it from a PowerShell prompt running as admin.

At which point you’ll be prompted to enter the following values:

  • SqlServer – the name of the SQL Server you installed against, in many cases this will simple be localhost
  • SqlAdminUser – a SQL Server user account that has the Sysadmin role, needs to be able to delete all the users and databases that where created for Sitecore
  • SqlAdminPassword – the password used by the above SQL account

To leverage a specific log file, you will need to include the path to it at script execution via the SIALogFile parameter.

After entering the required SQL connection and credentials the script will read the latest log file (or the file specified) and provide a review prompt for you.

At the prompt enter Y to continue and have a nice clean environment again for you next Sitecore adventure.

Go get the Script

You can grab the script from GitHub at: https://github.com/gillissm/TheCodeAttic.Sitecore.InstallUtilities/blob/version/SC9_3/Sitecore-Uninstall-SIA/Sitecore-Uninstall-SIA.ps1.

The script for a quick review:

<#
.SYNOPSIS
The script provides the minimal required data to uninstall an installation of Sitecore that was done via the Sitecore Install Assistant (SIA)
 
The script requires the user to proivde four parameters and retrieves the remaining data from the last SIA log file.

If your installation leveraged the defaul setup.exe.config provided by SIA then no edits to this file is required.

.DESCRIPTION
The script provides the minimal required data to uninstall an installation of Sitecore that was done via the Sitecore Install Assistant (SIA)
 
The script requires the user to proivde four parameters and retrieves the remaining data from the last SIA log file.

If your installation leveraged the defaul setup.exe.config provided by SIA then no edits to this file is required.

.PARAMETER SqlServer
The DNS name or IP of the SQL Instance that you installed against.

Required
String

.PARAMETER SqlAdminUser
A SQL user with sysadmin privileges

Required
String

.PARAMETER SqlAdminPassword
The password for SQLAdminUser.

Required
String

.PARAMETER SIALogFile
The full path to a specific log file that the uninstall should be based on. If not included will default to the most recent log file at "$env:USERPROFILE\sitecore.installassistant"

Optional
String

.EXAMPLE
.\Sitecore-Uninstall-SIA.ps1

You will then be prompted for required parameters.

.EXAMPLE
.\Sitecore-Uninstall-SIA.ps1 -SqlServer localhost -SqlAdminUser sa -SqlAdminPassword sa

.EXAMPLE
.\Sitecore-Uninstall-SIA.ps1 -SqlServer localhost -SqlAdminUser sa -SqlAdminPassword sa -SIALogFile "C:\Installs\sia-20191101.log"

#>

param(

    # The DNS name or IP of the SQL Instance.
    [Parameter(Mandatory = $true, HelpMessage = "The DNS name or IP of the SQL Instance")]
    [string]$SqlServer,
    # A SQL user with sysadmin privileges.
    [Parameter(Mandatory = $true, HelpMessage = "A SQL user with sysadmin privileges")]
    [string]$SqlAdminUser,
    # The password for $SQLAdminUser.
    [Parameter(Mandatory = $true, HelpMessage = "The password for SQLAdminUser.")]
    [string]$SqlAdminPassword,
    # The full path to a specific log file for uninstalling
    [Parameter(Mandatory = $false, HelpMessage = "The full path to a specific log file that the uninstall should be based on.")]
    [string]$SIALogFile=""
)

Import-Module SitecoreInstallFramework -RequiredVersion 2.2.0
Import-Module SitecoreFundamentals

if($SIALogFile -eq "" )
{
    # Retrieve the log file from the last run of SIA.
    $logfile = Get-ChildItem -Path "$env:USERPROFILE\sitecore.installassistant" -Filter "Sitecore-InstallConfiguration_*.txt" | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1

    $SIALogFile = Join-Path "$env:USERPROFILE\sitecore.installassistant" $logFile.Name
}

#The root folder with the WDP files and XP0-SingelDeveloper.json
$SCInstallRoot = ""
#The full path to the XP0-SingelDeveloper.json or similar custom configuration JSON.
$SCInstallConfig = ""

#Get Working Directory, SCInstallRoot
$workingDirectoryPath = Select-String -Path $SIALogFile -Pattern "^WorkingDirectory" -list
$whatIfLine = Select-String -Path $SIALogFile -Pattern "^WhatIf" -list
	
if($workingDirectoryPath.LineNumber -lt ($whatIfLine.LineNumber - 1))
{
	#is a wrapped line.
	$secondPart = Get-Content $SIALogFile | Select-Object -Index $workingDirectoryPath.LineNumber
	$secondPart = $secondPart.Trim()
	if($secondPart.Startswith("/"))
	{
		$SCInstallRoot = Join-Path ($workingDirectoryPath -split " :")[1].Trim() $secondPart
	}
	else{
		$SCInstallRoot = "{0} {1}" -f ($workingDirectoryPath -split " :")[1].Trim(),$secondPart		
	}        
}	
else {
	$SCInstallRoot = ($workingDirectoryPath -split " : ")[1].Trim()
}

#Get Install Configuration JSON
$confiugrationPath = Select-String -Path $SIALogFile -Pattern ".XP0-SingleDeveloper.json$" -list
$whatIfLine = Select-String -Path $SIALogFile -Pattern "^WhatIf" -list
	
 if (-Not $confiugrationPath.Line.StartsWith("Configuration")) {	
        #is a wrapped line
        $i = $devJsonConfig.LineNumber - 2
        $firstPath = Get-Content $SIALogFile | Select-Object -Index $i		
		if($confiugrationPath.Line.Trim().StartsWith("/"))
		{
			$SCInstallConfig = Join-Path ($firstPath -split " :")[1].Trim() $confiugrationPath.Line.Trim()	
		}
		else
		{
			$SCInstallConfig = "{0} {1}" -f ($firstPath -split " :")[1].Trim(),$confiugrationPath.Line.Trim()		
		}
        
    }
    else {
        $SCInstallConfig = ($confiugrationPath -split " : ")[1].Trim()
    }

# The URL of the Solr Server
$SolrUrl =  ((Select-String -Path $SIALogFile -Pattern "\[Requesting\].https.[/solr]{1}?" -list) -split "\[Requesting\]")[1].Trim()
# The name for the XConnect service.
$XConnectSiteName = ((Select-String -Path $SIALogFile -SimpleMatch -Pattern "[XConnectXP0_CreateWebsite]:[Create]") -split " ")[1]
# The Sitecore site instance name.
$SitecoreSiteName = ((Select-String -Path $SIALogFile -SimpleMatch -Pattern "[SitecoreXP0_CreateWebsite]:[Create]") -split " ")[1]
# Identity Server site name
$IdentityServerSiteName = ((Select-String -Path $SIALogFile -SimpleMatch -Pattern "[IdentityServer_CreateWebsite]:[Create]") -split " ")[1]
# The Prefix that will be used on SOLR, Website and Database instances.
if($SitecoreSiteName -eq $null)
{
    $SitecoreSiteName = Read-Host -Prompt "The site name could not be retrieved from the log file. Enter the Sitecore name that is to be uninstalled."
}

if($SitecoreSiteName.EndsWith("sc.dev.local"))
{
	$Prefix = $SitecoreSiteName.Replace("sc.dev.local", "")
}
else{
    $Prefix = Read-Host -Prompt "For the site named '$SitecoreSiteName', what is the prefix used for databases and Solr indexes?"
}

Write-Host -ForegroundColor DarkGreen "------ Uninstall Parameters ------" 

Write-Host "    SIA Log File............... $SIALogFile" 
Write-Host "    Solr URL................... $SolrUrl" 
Write-Host "    Prefix..................... $Prefix" 
Write-Host "    Sitecore Site Name......... $SitecoreSiteName" 
Write-Host "    xConnect Site Name......... $XConnectSiteName" 
Write-Host "    Identity Server Site Name.. $IdentityServerSiteName" 
Write-Host "    Install Working Path....... $SCInstallRoot"
Write-Host "    Install Configuration Path. $SCInstallConfig"


$question = "Do you want to proceed with uninstalling $SitecoreSiteName?"
$choices = '&Yes', '&No'

$decision = $Host.UI.PromptForChoice(" ", $question, $choices, 1)
if ($decision -eq 0) {    
    $singleDeveloperParams = @{
        Path                          = $SCInstallConfig
        SqlServer                     = $SqlServer
        SqlAdminUser                  = $SqlAdminUser
        SqlAdminPassword              = $SqlAdminPassword
        SolrUrl                       = $SolrUrl    
        Prefix                        = $Prefix
        XConnectCertificateName       = $XConnectSiteName
        IdentityServerCertificateName = $IdentityServerSiteName
        IdentityServerSiteName        = $IdentityServerSiteName    
        XConnectSiteName              = $XConnectSiteName
        SitecoreSitename              = $SitecoreSiteName
    }

    Push-Location $SCInstallRoot
    Uninstall-SitecoreConfiguration @singleDeveloperParams *>&1 | Tee-Object XP0-SingleDeveloper-Uninstall.log
    Pop-Location

    Write-Host "Uninstallation has completed for $SitecoreSiteName" -ForegroundColor DarkGreen
}
else {
    Write-Host "Uninstalling of $SitecoreSiteName has been cancelled." -ForegroundColor  DarkYellow
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.