Forked from himadric/Remove-Sitecore9-Instance.ps1
Last active
December 20, 2021 19:44
-
-
Save fettesps/b835666e00055d18793bccb78208c61c to your computer and use it in GitHub Desktop.
A Powershell script for removing Sitecore 9 instance from local machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
#This script is created to remove Sitecore 9 instance from local machine. | |
#This will not uninstall Solr but, it will remove Solr collections. | |
#The script looks at the Prefix parameter and find out all instances and remove them. | |
#Change the default values before using the script. Or, pass the values as parameters. | |
#Usage: .\Remove-Sitecore9-Instance.ps1 -Prefix "xp0" | |
#Declaimer: This is a destructive script, means, it deletes files, databases etc. | |
#Use it at your own risk. | |
#License: MIT | |
############################################################################################ | |
param( | |
#Website related parameters | |
[string]$Prefix ='xp0', | |
[string]$WebsiteOhysicalRootPath = 'C:\inetpub\wwwroot\', | |
#Database related parameters | |
#[string]$SQLInstanceName = '', | |
[string]$SQLUsername = 'sa', | |
[string]$SQLPassword = '********', | |
#Certificate related parameters | |
[string]$CertificateRootStore = 'Cert:\Localmachine\Root', | |
[string]$CertificatePersonalStore = 'Cert:\Localmachine\My', | |
[string]$XConnectCertName = "$Prefix.xconnect", | |
[string]$XConnectClientCertName = "$Prefix.xconnect_client", | |
[string]$SitecoreRootCertName = 'DO_NOT_TRUST_SitecoreRootCert', | |
[string]$SitecoreFundamentalsRootCertName = 'DO_NOT_TRUST_SitecoreFundamentalsRoot', | |
[string]$CertPath = 'C:\Certificates', | |
#Solr related parameters | |
[string]$SolrPath = '[path]\solr-7.2.1' | |
) | |
$XConnectWebsiteName = "$Prefix.xconnect" | |
$IdentityServerName = "$Prefix.identityserver" | |
$SitecoreWebsiteName = "$Prefix.sc" | |
$XConnectWebsitePhysicalPath = "$WebsiteOhysicalRootPath$Prefix.xconnect" | |
$IdentityWebsitePhysicalPath = "$WebsiteOhysicalRootPath$Prefix.identityserver" | |
$SitecoreWebsitePhysicalPath = "$WebsiteOhysicalRootPath$Prefix.sc" | |
$HostFileLocation = "c:\windows\system32\drivers\etc\hosts" | |
$MarketingAutomationService = "$Prefix.xconnect-MarketingAutomationService" | |
$ProcessingEngineService = "$Prefix.xconnect-ProcessingEngineService" | |
$IndexWorker = "$Prefix.xconnect-IndexWorker" | |
Write-Host -foregroundcolor Green "Starting Sitecore 9 instance removal..." | |
# REMOVE SITES (IIS) | |
#Remove Sitecore website | |
if([bool](Get-Website $SitecoreWebsiteName)) { | |
Write-host -foregroundcolor Green "Deleting Website $SitecoreWebsiteName" | |
Remove-WebSite -Name $SitecoreWebsiteName | |
Write-host -foregroundcolor Green "Deleting App Pool $SitecoreWebsiteName" | |
Remove-WebAppPool $SitecoreWebsiteName | |
} | |
else { | |
Write-host -foregroundcolor Red "Website $SitecoreWebsiteName does not exist." | |
} | |
#Remove XConnect website | |
if([bool](Get-Website $XConnectWebsiteName)) { | |
Write-host -foregroundcolor Green "Deleting Website $XConnectWebsiteName" | |
Remove-WebSite -Name $XConnectWebsiteName | |
Write-host -foregroundcolor Green "Deleting App Pool $XConnectWebsiteName" | |
Remove-WebAppPool $XConnectWebsiteName | |
} | |
else { | |
Write-host -foregroundcolor Red "Website $XConnectWebsiteName does not exist." | |
} | |
#Remove Identity Server website | |
if([bool](Get-Website $IdentityServerName)) { | |
Write-host -foregroundcolor Green "Deleting Website $IdentityServerName" | |
Remove-WebSite -Name $IdentityServerName | |
Write-host -foregroundcolor Green "Deleting App Pool $IdentityServerName" | |
Remove-WebAppPool $IdentityServerName | |
} | |
else { | |
Write-host -foregroundcolor Red "Website $XConnectWebsiteName does not exist." | |
} | |
#Remove hosts entries | |
if([bool]((get-content $HostFileLocation) -match $Prefix)) { | |
Write-Host -foregroundcolor Green "Deleting hosts entries." | |
(get-content $HostFileLocation) -notmatch $Prefix | Out-File $HostFileLocation | |
} | |
else { | |
Write-Host -foregroundcolor Red "No hosts entries found." | |
} | |
# Stop and remove Marketing Automation Service | |
Get-WmiObject -Class Win32_Service -Filter "Name='$MarketingAutomationService'" | Remove-WmiObject | |
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='$MarketingAutomationService'" | |
if($Service) { | |
Get-Process -Name "maengine" | Stop-Process -Force | |
Write-Host -foregroundcolor Green "Deleting " $MarketingAutomationService | |
$Service.StopService() | |
$Service.delete() | |
} | |
else { | |
Write-Host -foregroundcolor Red $MarketingAutomationService " service does not exist." | |
} | |
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='$IndexWorker'" | |
if($Service) { | |
Write-Host -foregroundcolor Green "Deleting " $IndexWorker | |
$Service.StopService() | |
$Service.delete() | |
} | |
else { | |
Write-Host -foregroundcolor Red $IndexWorker " service does not exist." | |
} | |
# Stop and remove Processing Engine Service | |
Get-WmiObject -Class Win32_Service -Filter "Name='$ProcessingEngineService'" | Remove-WmiObject | |
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='$ProcessingEngineService'" | |
if($Service) { | |
Get-Process -Name "maengine" | Stop-Process -Force | |
Write-Host -foregroundcolor Green "Deleting " $ProcessingEngineService | |
$Service.StopService() | |
$Service.delete() | |
} | |
else { | |
Write-Host -foregroundcolor Red $ProcessingEngineService " service does not exist." | |
} | |
# Stop and Remove Index Worker Service | |
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='$IndexWorker'" | |
if($Service) { | |
Write-Host -foregroundcolor Green "Deleting " $IndexWorker | |
$Service.StopService() | |
$Service.delete() | |
} | |
else { | |
Write-Host -foregroundcolor Red $IndexWorker " service does not exist." | |
} | |
#Remove Sitecore Files | |
if (Test-Path $SitecoreWebsitePhysicalPath) { | |
Remove-Item -path $SitecoreWebsitePhysicalPath\* -recurse | |
Remove-Item -path $SitecoreWebsitePhysicalPath | |
Write-host -foregroundcolor Green $SitecoreWebsitePhysicalPath " Deleted" | |
[System.Threading.Thread]::Sleep(1500) | |
} else { | |
Write-host -foregroundcolor Red $SitecoreWebsitePhysicalPath " Does not exist" | |
} | |
#Remove XConnect files | |
if (Test-Path $XConnectWebsitePhysicalPath) { | |
Remove-Item -path $XConnectWebsitePhysicalPath\* -recurse -Force -ErrorAction SilentlyContinue | |
Remove-Item -path $XConnectWebsitePhysicalPath -Force | |
Write-host -foregroundcolor Green $XConnectWebsitePhysicalPath " Deleted" | |
[System.Threading.Thread]::Sleep(1500) | |
} else { | |
Write-host -foregroundcolor Red $XConnectWebsitePhysicalPath " Does not exist" | |
} | |
#Remove Identity Server files | |
if (Test-Path $IdentityWebsitePhysicalPath) { | |
Remove-Item -path $IdentityWebsitePhysicalPath\* -recurse -Force -ErrorAction SilentlyContinue | |
Remove-Item -path $IdentityWebsitePhysicalPath -Force | |
Write-host -foregroundcolor Green $IdentityWebsitePhysicalPath " Deleted" | |
[System.Threading.Thread]::Sleep(1500) | |
} else { | |
Write-host -foregroundcolor Red $IdentityWebsitePhysicalPath " Does not exist" | |
} | |
#Remove SQL Databases | |
import-module SqlServer | |
$DBListQuery = "SELECT * FROM sys.databases WHERE Name LIKE '" + $Prefix + "_%';" | |
$DBList = invoke-sqlcmd -ServerInstance "." -U "$SQLUsername" -P "$SQLPassword" -Query $DBListQuery | |
ForEach($DB in $DBList) { | |
Write-host -foregroundcolor Green "Deleting Database " $DB.Name | |
$AlterQuery = "ALTER DATABASE [" + $DB.Name + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;" | |
$DropQuery = "DROP DATABASE [" + $DB.Name + "];" | |
invoke-sqlcmd -ServerInstance "." -U "$SQLUsername" -P "$SQLPassword" -Query $AlterQuery | |
invoke-sqlcmd -ServerInstance "." -U "$SQLUsername" -P "$SQLPassword" -Query $DropQuery | |
} | |
#Remove Certificates | |
if([bool](Get-ChildItem -Path $CertificateRootStore -dnsname $SitecoreRootCertName)) { | |
Write-host -foregroundcolor Green "Deleting certificate " $SitecoreRootCertName | |
Get-ChildItem -Path $CertificateRootStore -dnsname $SitecoreRootCertName | Remove-Item | |
} | |
else { | |
Write-host -foregroundcolor Red "Certificate " $SitecoreRootCertName " does not exist." | |
} | |
if([bool](Get-ChildItem -Path $CertificateRootStore -dnsname $SitecoreFundamentalsRootCertName)) { | |
Write-host -foregroundcolor Green "Deleting certificate " $SitecoreFundamentalsRootCertName | |
Get-ChildItem -Path $CertificateRootStore -dnsname $SitecoreFundamentalsRootCertName | Remove-Item | |
} | |
else { | |
Write-host -foregroundcolor Red "Certificate " $SitecoreFundamentalsRootCertName " does not exist." | |
} | |
if([bool](Get-ChildItem -Path $CertificatePersonalStore -dnsname $XConnectCertName)) { | |
Write-host -foregroundcolor Green "Deleting certificate " $XConnectCertName | |
Get-ChildItem -Path $CertificatePersonalStore -dnsname $XConnectCertName | Remove-Item | |
} | |
else { | |
Write-host -foregroundcolor Red "Certificate " $XConnectCertName " does not exist." | |
} | |
if([bool](Get-ChildItem -Path $CertificatePersonalStore -dnsname $XConnectClientCertName)) { | |
Write-host -foregroundcolor Green "Deleting certificate " $XConnectClientCertName | |
Get-ChildItem -Path $CertificatePersonalStore -dnsname $XConnectClientCertName | Remove-Item | |
} | |
else { | |
Write-host -foregroundcolor Red "Certificate " $XConnectClientCertName " does not exist." | |
} | |
if (Test-Path $CertPath) { | |
Remove-Item -path $CertPath\* -recurse | |
Remove-Item -path $CertPath | |
Write-host -foregroundcolor Green $CertPath " Deleted" | |
[System.Threading.Thread]::Sleep(1500) | |
} else { | |
Write-host -foregroundcolor Red $CertPath " Does not exist" | |
} | |
# Remove Solr Cores | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_core_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_fxm_master_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_fxm_web_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_marketing_asset_index_master") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_marketing_asset_index_web") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_marketingdefinitions_master") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_marketingdefinitions_web") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_suggested_test_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_testing_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_web_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_master_index") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_xdb") | |
& "$SolrPath\bin\solr.cmd" delete -c ($Prefix + "_xdb_rebuild") | |
Write-Host -foregroundcolor Green "Finished Sitecore 9 instance removal..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed a few typos in the original, plus updated to use SqlServer instead of sqlps and also added logic to remove identity server as well