Skip to content

Instantly share code, notes, and snippets.

@jisha86swamy
Last active February 28, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jisha86swamy/71f25948fb34eab515d3d8e4b29d4214 to your computer and use it in GitHub Desktop.
Save jisha86swamy/71f25948fb34eab515d3d8e4b29d4214 to your computer and use it in GitHub Desktop.
Script file to install sitecore 9 instance
#define parameters
$prefix = "HackathonSite"
$SqlServer = "[Sql server name]"
$SqlAdminUser = "[Admin user name]"
$SqlAdminPassword="[AdminPassword]"
$SolrVersionNumber ='6.6.2'
$KeyToolFilePath ='C:\Program Files\Java\jre1.8.0_162\bin\keytool.exe'
$SQLCommandBinFolderPath ='C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn'
$SitecoreBuildVersionVersion = '9.0.1 rev. 171219'
$XConnectCollectionService = "$prefix.xconnect"
$SolrVersion ="solr-$SolrVersionNumber"
$SolrPortNumber="8547"
$SolrInstallPath=""
$sitecoreSiteName = "$prefix.sc"
$output = "$PSScriptRoot\$SolrVersion.zip"
$KeyToolFilePath ='C:\Program Files\Java\jre1.8.0_162\bin\keytool.exe'
$solrdownloadurl = "http://archive.apache.org/dist/lucene/solr/$SolrVersionNumber/$SolrVersion.zip"
$SolrServiceName = "HackathonTemp-$SolrVersion" #change this if you want to customise service name
$SolrUrl = "https://localhost:$SolrPortNumber/solr"
$SolrRoot = "$PSScriptRoot\$SolrVersion"
$start_time = Get-Date
Write-Host '*******Setting up preinstallation steps*******'
$PSScriptOutputRoot ="$PSScriptRoot\Output"
$NSSMZipPath = "$PSScriptRoot\nssm-2.24.zip"
$NSSMUnZipPath = "$PSScriptRoot"
$NSSMUnZipFolder = "$PSScriptRoot\nssm-2.24"
# $SolrServiceName = "$SolrVersion"
if (Get-Service $SolrServiceName -ErrorAction SilentlyContinue) {
Stop-Service $SolrServiceName
powershell -Command "$NSSMUnZipFolder\win64\nssm remove $SolrServiceName confirm"
}
if(!(Test-Path $output)) {
Write-Host "Getting Solr zip file...$solrdownloadurl as $output"
try
{
Invoke-WebRequest -Uri $solrdownloadurl -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
} catch{
Write-Host '*******This version is not availble for download*******'
}
}
if((Test-Path $PSScriptOutputRoot)) {
Remove-Item $PSScriptOutputRoot -recurse -Exclude "Output"
}
else
{
New-Item -ItemType Directory -Force -Path $PSScriptOutputRoot
}
$NSSMurl = "https://nssm.cc/release/nssm-2.24.zip"
$NSSoutput = "$PSScriptRoot\nssm-2.24.zip"
$start_time = Get-Date
if(!(Test-Path $NSSoutput)) {
Write-Host 'Getting NSSM zip file...'
Invoke-WebRequest -Uri $NSSMurl -OutFile $NSSoutput
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
}
$SolrZipPath = "$PSScriptRoot\$SolrVersion.zip"
$SolrUnZipPath = "$PSScriptRoot"
$SolrHome = "$PSScriptRoot\$SolrVersion"
if($SolrInstallPath -ne "")
{
$SolrHome = "$SolrInstallPath\$SolrVersion"
$SolrUnZipPath = "$SolrInstallPath"
}
if((Test-Path $SolrHome)) {
Write-Host "Removing existing solr extract folder $SolrHome..."
Remove-Item -path $SolrHome -Recurse
}
Write-Host "Extracting Solr zip file... $SolrZipPath to $SolrUnZipPath"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($SolrZipPath, $SolrUnZipPath)
if((Test-Path $NSSMUnZipFolder)) {
Write-Host "Removing existing NSSM extract folder $NSSMUnZipFolder..."
Remove-Item -path $NSSMUnZipFolder -Recurse
}
Write-Host 'Extracting NSSM zip file...'
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($NSSMZipPath, $NSSMUnZipPath)
Write-Host ''
Write-Host 'Setting Up Solr Service...'
$NSSMbinPath = "$PSScriptRoot\nssm-2.24\win64"
if (Get-Service $SolrServiceName -ErrorAction SilentlyContinue) {
Write-Host "Setting SSL for SOLR service"
Write-Host "Stopping $SolrServiceName"
Stop-Service $SolrServiceName
powershell -Command "$NSSMUnZipFolder\win64\nssm remove $SolrServiceName confirm"
powershell -Command "$NSSMUnZipFolder\win64\nssm install $SolrServiceName '$SolrHome\bin\solr.cmd' 'start -f -p $SolrPortNumbers"
Start-Service $SolrServiceName
} else {
Write-Host "Setting SSL for SOLR service"
Write-Host "$Create service $SolrServiceName"
powershell -Command "$NSSMUnZipFolder\win64\nssm install $SolrServiceName '$SolrHome\bin\Solr.cmd' 'start -f -p $SolrPortNumber'"
Start-Service $SolrServiceName
}
Write-Host "Setting SSL for SOLR service"
$JKSPath="$PSScriptRoot\solr-ssl.keystore.p12"
# remove existing jks and p12 files
if((Test-Path $JKSPath)) {
Write-Host "removing existing keys"
Remove-Item -path $PSScriptRoot\solr-ssl.keystore.jks
Remove-Item -path $PSScriptRoot\solr-ssl.keystore.p12
}
else
{
Write-Host "p12 is not available and it will be regenerated"
}
$JKSPath="$PSScriptRoot\solr-ssl.keystore.jks"
if((Test-Path $JKSPath)) {
Write-Host "removing existing keys"
Remove-Item -path $PSScriptRoot\solr-ssl.keystore.jks
Remove-Item -path $PSScriptRoot\solr-ssl.keystore.p12
}
else
{
Write-Host "jks is not available and it will be regenerated"
}
Write-Host "Generating Keys $KeyToolFilePath"
./solrssl -KeyToolFilePath $KeyToolFilePath
$SolrCommands =@"
set SOLR_SSL_KEY_STORE=$SolrHome\bin\etc\solr-ssl.keystore.jks
set SOLR_SSL_KEY_STORE_PASSWORD=secret
set SOLR_SSL_TRUST_STORE=$SolrHome\bin\etc\solr-ssl.keystore.jks
set SOLR_SSL_TRUST_STORE_PASSWORD=secret
"@
$SolrBINCommand ="$SolrHome\bin\solr.in.cmd"
$SolrEtcFolder ="$SolrHome\bin\etc"
if(!(Test-Path $SolrEtcFolder)) {
New-Item -ItemType Directory -Force -Path $SolrEtcFolder
}
Write-Host 'Copying certificate files to Solr folder...'
Copy-Item $PSScriptRoot\solr-ssl.keystore.jks $SolrEtcFolder
Copy-Item $PSScriptRoot\solr-ssl.keystore.p12 $SolrEtcFolder
Add-Content $SolrBINCommand $SolrCommands
$SolrDefaultConfigsPath="$SolrHome\server\solr\configsets\_default"
$SolrDefaultConfigs ="basic_configs"
if((Test-Path $SolrDefaultConfigsPath)) {
$SolrDefaultConfigs ="_default"
}
$SolrConfigFolder ="$SolrHome\server\solr\configsets\$SolrDefaultConfigs\conf"
Write-Host "Restarting solr service $SolrUrl"
if (Get-Service $SolrServiceName -ErrorAction SilentlyContinue) {
powershell -Command "$NSSMUnZipFolder\win64\nssm stop $SolrServiceName "
}
powershell -Command "$NSSMUnZipFolder\win64\nssm start $SolrServiceName "
Start-Sleep -s 20
Write-Host "************HTTPS Solr Service is started successfully"
Write-Host "You can access service at $SolrUrl"
$SitecorePackage = "$PSScriptRoot\Sitecore $SitecoreBuildVersionVersion (WDP XP0 packages).zip"
$SitecorePackageUnZipPath = "$PSScriptOutputRoot"
Write-Host "Connection to sql servr"
$ConnectionString = "Data Source=$SqlServer;Initial Catalog=master;User Id=$SqlAdminUser;Password=$SqlAdminPassword;"
$connection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString);
$command = New-Object System.Data.SqlClient.SqlCommand;
$command.Connection = $connection;
$command.CommandType = [System.Data.CommandType]::Text;
$command.CommandText = @"
sp_configure 'contained database authentication',1;
"@;
$connection.open();
$reader = $command.ExecuteReader()
While($reader.Read()){
Write-Host 'Copying certificate file to Solr folder...$reader.'
}
Write-Host 'Copying certificate file to Solr folder...$reader.'
$connection.Close()
$reader.Close()
$command.CommandText = @"
RECONFIGURE;
"@;
$connection.open();
$reader = $command.ExecuteReader()
While($reader.Read()){
Write-Host 'Copying certificate file to Solr folder...$reader.'
}
Write-Host 'Copying certificate file to Solr folder...$reader.'
$connection.Close()
$reader.Close()
Write-Host "**********Database contained authentication has been set succssfully*******************"
Write-Host 'Extracting $SitecorePackage XP0 packages zip file...'
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($SitecorePackage, $SitecorePackageUnZipPath)
$SitecorePackage = "$PSScriptOutputRoot\XP0 Configuration files $SitecoreBuildVersionVersion.zip"
Write-Host 'Extracting zip file...'
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($SitecorePackage, $SitecorePackageUnZipPath)
Write-Host "Copy Config files to setup path"
#Copy-Item $PSScriptOutputRoot\sitecore-solr.json $PSScriptRoot
#Copy-Item $PSScriptOutputRoot\sitecore-XP0.json $PSScriptRoot
#Copy-Item $PSScriptOutputRoot\xconnect-createcert.json $PSScriptRoot
#Copy-Item $PSScriptOutputRoot\xconnect-solr.json $PSScriptRoot
#Copy-Item $PSScriptOutputRoot\xconnect-xp0.json $PSScriptRoot
# Add the Sitecore MyGet repository to PowerShell
Register-PSRepository -Name SitecoreGallery -SourceLocation https://sitecore.myget.org/F/sc-powershell/api/v2
# Install the Sitecore Install Framwork module
if (Get-Module -ListAvailable -Name SitecoreInstallFramework)
{
Write-Host "SitecoreInstallFramework module exists"
}
else
{
Install-Module SitecoreInstallFramework -Confirm
}
# Install the Sitecore Fundamentals module (provides additional functionality for local installations like creating self-signed certificates)
if (Get-Module -ListAvailable -Name SitecoreFundamentals)
{
Write-Host "SitecoreInstallFramework module exists"
}
else
{
Install-Module SitecoreFundamentals -Confirm
}
# Import the modules into your current PowerShell context (if necessary)
Import-Module SitecoreFundamentals
Import-Module SitecoreInstallFramework
# ./install
<#
$solrParams = @{
Path = "$PSScriptOutputRoot\sitecore-solr.json"
SolrUrl = $SolrUrl
SolrRoot = $SolrRoot
SolrService = $SolrServiceName
CorePrefix = $prefix
}
#Install-SitecoreConfiguration @solrParams
#>
#install client certificate for xconnect
$certParams = @{
Path = "$PSScriptOutputRoot\xconnect-createcert.json"
CertificateName = "$prefix.xconnect_client"
}
Install-SitecoreConfiguration @certParams -Verbose
#install solr cores for xdb
$solrParams =
@{
Path = "$PSScriptOutputRoot\xconnect-solr.json"
SolrUrl = $SolrUrl
SolrRoot = $SolrRoot
SolrService = $SolrServiceName
CorePrefix = $prefix
BaseConfig = $SolrDefaultConfigs
}
Install-SitecoreConfiguration @solrParams -Verbose
#deploy xconnect instance
$xconnectParams = @{
Path = "$PSScriptOutputRoot\xconnect-xp0.json"
Package = "$PSScriptOutputRoot\Sitecore $SitecoreBuildVersionVersion (OnPrem)_xp0xconnect.scwdp.zip"
LicenseFile = "$PSScriptRoot\license.xml"
Sitename = $XConnectCollectionService
XConnectCert = $certParams.CertificateName
SqlDbPrefix = $prefix
SqlServer = $SqlServer
SqlAdminUser = $SqlAdminUser
SqlAdminPassword = $SqlAdminPassword
SolrCorePrefix = $prefix
SolrURL = $SolrUrl
}
Install-SitecoreConfiguration @xconnectParams -Verbose
#install solr cores for sitecore $solrParams =
$solrParams = @{
Path = "$PSScriptOutputRoot\sitecore-solr.json"
SolrUrl = $SolrUrl
SolrRoot = $SolrRoot
SolrService = $SolrServiceName
CorePrefix = $prefix
}
Install-SitecoreConfiguration @solrParams
#install sitecore instance
$xconnectHostName = "$prefix.xconnect"
$sitecoreParams =
@{
Path = "$PSScriptOutputRoot\sitecore-XP0.json"
Package = "$PSScriptOutputRoot\Sitecore $SitecoreBuildVersionVersion (OnPrem)_single.scwdp.zip"
LicenseFile = "$PSScriptRoot\license.xml"
SqlDbPrefix = $prefix
SqlServer = $SqlServer
SqlAdminUser = $SqlAdminUser
SqlAdminPassword = $SqlAdminPassword
SolrCorePrefix = $prefix
SolrUrl = $SolrUrl
XConnectCert = $certParams.CertificateName
Sitename = $sitecoreSiteName
XConnectCollectionService = "https://$XConnectCollectionService"
}
Install-SitecoreConfiguration @sitecoreParams
Write-Host '*****Post Installation Task started***********e...'
Write-Host "Executing sql scripts... $SQLCommandBinFolderPath\sqlcmd -S $SqlServer -U $SqlAdminUser -P $SqlAdminPassword -i $PSScriptRoot\PostInstallScripts.sql"
$InputFile = @"
$SQLCommandBinFolderPath\sqlcmd -S $SqlServer -U $SqlAdminUser -P $SqlAdminPassword -i
"@
Invoke-Sqlcmd -InputFile $PSScriptRoot\PostInstallScripts.sql
:SETVAR DatabasePrefix SC9DemoXP0
:SETVAR UserName collectionuser
:SETVAR Password Test12345
:SETVAR ShardMapManagerDatabaseNameSuffix _Xdb.Collection.ShardMapManager
:SETVAR Shard0DatabaseNameSuffix _Xdb.Collection.Shard0
:SETVAR Shard1DatabaseNameSuffix _Xdb.Collection.Shard1
GO
IF(SUSER_ID('$(UserName)') IS NULL)
BEGIN
CREATE LOGIN [$(UserName)] WITH PASSWORD = '$(Password)';
END;
GO
USE [$(DatabasePrefix)$(ShardMapManagerDatabaseNameSuffix)]
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$(UserName)')
BEGIN
CREATE USER [$(UserName)] FOR LOGIN [$(UserName)]
GRANT SELECT ON SCHEMA :: __ShardManagement TO [$(UserName)]
GRANT EXECUTE ON SCHEMA :: __ShardManagement TO [$(UserName)]
END;
GO
USE [$(DatabasePrefix)$(Shard0DatabaseNameSuffix)]
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$(UserName)')
BEGIN
CREATE USER [$(UserName)] FOR LOGIN [$(UserName)]
EXEC [xdb_collection].[GrantLeastPrivilege] @UserName = '$(UserName)'
END;
GO
USE [$(DatabasePrefix)$(Shard1DatabaseNameSuffix)]
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$(UserName)')
BEGIN
CREATE USER [$(UserName)] FOR LOGIN [$(UserName)]
EXEC [xdb_collection].[GrantLeastPrivilege] @UserName = '$(UserName)'
END;
GO
USE master;
ALTER DATABASE xp0_Core SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_Core ;
ALTER DATABASE xp0_ExperienceForms SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_ExperienceForms
ALTER DATABASE xp0_MarketingAutomation SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_MarketingAutomation
ALTER DATABASE xp0_Master SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_Master
ALTER DATABASE [xp0_Processing.Pools] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [xp0_Processing.Pools]
ALTER DATABASE [xp0_Processing.Tasks] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [xp0_Processing.Tasks]
ALTER DATABASE xp0_ReferenceData SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_ReferenceData
ALTER DATABASE xp0_Reporting SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_Reporting
ALTER DATABASE xp0_Web SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE xp0_Web
ALTER DATABASE [xp0_Xdb.Collection.Shard0] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [xp0_Xdb.Collection.Shard0]
ALTER DATABASE [xp0_Xdb.Collection.Shard1] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [xp0_Xdb.Collection.Shard1]
ALTER DATABASE [xp0_Xdb.Collection.ShardMapManager] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [xp0_Xdb.Collection.ShardMapManager]
param(
[string]$KeystoreFile = 'solr-ssl.keystore.jks',
[string]$KeystorePassword = 'secret',
[string]$SolrDomain = 'localhost',
[switch]$Clobber
)
$ErrorActionPreference = 'Stop'
### PARAM VALIDATION
if($KeystorePassword -ne 'secret') {
Write-Error 'The keystore password must be "secret", because Solr apparently ignores the parameter'
}
if((Test-Path $KeystoreFile)) {
if($Clobber) {
Write-Host "Removing $KeystoreFile..."
Remove-Item $KeystoreFile
} else {
$KeystorePath = Resolve-Path $KeystoreFile
Write-Error "Keystore file $KeystorePath already existed. To regenerate it, pass -Clobber."
}
}
$P12Path = [IO.Path]::ChangeExtension($KeystoreFile, 'p12')
if((Test-Path $P12Path)) {
if($Clobber) {
Write-Host "Removing $P12Path..."
Remove-Item $P12Path
} else {
$P12Path = Resolve-Path $P12Path
Write-Error "Keystore file $P12Path already existed. To regenerate it, pass -Clobber."
}
}
try {
$keytool = (Get-Command 'keytool.exe').Source
} catch {
$keytool = 'C:\Program Files\Java\jre1.8.0_151\bin\keytool.exe'
if([string]::IsNullOrEmpty($keytool) -or -not (Test-Path $keytool)) {
Write-Error "Keytool path was invalid."
}
}
### DOING STUFF
Write-Host ''
Write-Host 'Generating JKS keystore...'
& $keytool -genkeypair -alias solr-ssl -keyalg RSA -keysize 2048 -keypass $KeystorePassword -storepass $KeystorePassword -validity 9999 -keystore $KeystoreFile -ext SAN=DNS:$SolrDomain,IP:127.0.0.1 -dname "CN=$SolrDomain, OU=Organizational Unit, O=Organization, L=Location, ST=State, C=Country"
Write-Host ''
Write-Host 'Generating .p12 to import to Windows...'
& $keytool -importkeystore -srckeystore $KeystoreFile -destkeystore $P12Path -srcstoretype jks -deststoretype pkcs12 -srcstorepass $KeystorePassword -deststorepass $KeystorePassword
Write-Host ''
Write-Host 'Trusting generated SSL certificate...'
$secureStringKeystorePassword = ConvertTo-SecureString -String $KeystorePassword -Force -AsPlainText
$root = Import-PfxCertificate -FilePath $P12Path -Password $secureStringKeystorePassword -CertStoreLocation Cert:\LocalMachine\Root
Write-Host 'SSL certificate is now locally trusted. (added as root CA)'
Write-Host ''
Write-Host '########## NEXT STEPS ##########' -ForegroundColor Green
Write-Host ''
Write-Host '1. Copy your keystore to $SOLR_HOME\server\etc (MUST be here)' -ForegroundColor Green
if(-not $KeystoreFile.EndsWith('solr-ssl.keystore.jks')) {
Write-Warning 'Your keystore file is not named "solr-ssl.keystore.jks"'
Write-Warning 'Solr requires this exact name, so make sure to rename it before use.'
}
$KeystorePath = Resolve-Path $KeystoreFile
Write-Host ''
Write-Host '2. Add the following lines to your solr.in.cmd:' -ForegroundColor Green
Write-Host ''
Write-Host "set SOLR_SSL_KEY_STORE=etc/solr-ssl.keystore.jks" -ForegroundColor Yellow
Write-Host "set SOLR_SSL_KEY_STORE_PASSWORD=$KeystorePassword" -ForegroundColor Yellow
Write-Host "set SOLR_SSL_TRUST_STORE=etc/solr-ssl.keystore.jks" -ForegroundColor Yellow
Write-Host "set SOLR_SSL_TRUST_STORE_PASSWORD=$KeystorePassword" -ForegroundColor Yellow
Write-Host ''
Write-Host 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment