Pipelines/Steps/Install/OnPrem/Sitecore/XP/Scaled/9.1/Install-CM.ps1

$ErrorActionPreference = "Stop"

$prefix = $SAFConfiguration.prefix
$license = $SAFConfiguration.license
$sitecoreAdminUserPassword = $SAFConfiguration.sitecore.adminPassword
$sqlServer = $SAFConfiguration.sql.serverName
$sqlUser = $SAFConfiguration.sql.adminUsername
$sqlAdminPassword = $SAFConfiguration.sql.adminPassword
$sqlSitecorePassword = $SAFConfiguration.sql.sitecorePassword
$identityServerUrl = "https://$($SAFConfiguration.identityServer.hostName)"
$identityServerSecret = $SAFConfiguration.identityServer.clientSecret
$edsProvider = "CustomSMTP"
if (![string]::IsNullOrEmpty($SAFConfiguration.exm.edsProviderURL)) {
    $edsProvider = $SAFConfiguration.exm.edsProviderURL
}
$telerikEncryptionKey = $SAFConfiguration.telerikEncryptionKey
$solrURL = $SAFSolrServiceURL
$processingServiceURL = $SAFConfiguration.processing.serviceURL
$reportingServiceURL = $SAFConfiguration.reporting.serviceURL
$reportingServiceApiKey = $SAFConfiguration.reporting.serviceApiKey
$xConnectCollectionSearchService = $SAFConfiguration.xConnect.collectionSearchServiceURL
$xConnectCortexReportingService = $SAFConfiguration.xConnect.cortexReportingServiceURL
$xConnectReferenceDataService = $SAFConfiguration.xDB.referenceDataServiceURL
$marketingAutomationOperationsService = $SAFConfiguration.xDB.automationOperationsServiceURL
$marketingAutomationReportingService = $SAFConfiguration.xDB.automationReportingServiceURL
$exmCryptographicKey = $SAFConfiguration.exm.cryptographicKey
$exmAuthenticationKey = $SAFConfiguration.exm.authenticationKey
$package = Get-ChildItem -Path "$SAFInstallPackageDir\*" -Include *cm.scwdp.zip*
$sifConfiguration = GetSIFConfiguration -Name "sitecore-XP1-cm.json" -ContextPath "$PSScriptRoot"

$clientCert = BuildSitecoreClientCertName -Prefix $prefix
$serverCert = BuildSitecoreServerCertName -Prefix $prefix

$hostNames = $SAFConfiguration.sitecore.hostNames
$siteName = $hostNames[0]
$installDir = $SAFConfiguration.sitecore.installDir

Write-Output "Install Sitecore CM started..."

$dbs = @("$($prefix)_Core", "$($prefix)_Master", "$($prefix)_Web", "$($prefix)_ExperienceForms", "$($prefix)_EXM.Master")
DeleteDatabases -SqlServer $sqlServer -Databases $dbs -Username $sqlUser -Password $sqlAdminPassword

$sitecoreParams = @{
    Package = $package.FullName
    Path = $sifConfiguration
    LicenseFile = $license
    SqlDbPrefix = $prefix
    SolrCorePrefix = $prefix
    SSLCert = $serverCert
    XConnectCert = $clientCert
    Sitename = $siteName
    SitecoreAdminPassword = $sitecoreAdminUserPassword
    SqlAdminUser = $sqlUser
    SqlAdminPassword = $sqlAdminPassword
    SqlCoreUser  = "$($prefix)_coreuser"
    SqlCorePassword = $sqlSitecorePassword
    SqlSecurityUser ="$($prefix)_securityuser"
    SqlSecurityPassword = $sqlSitecorePassword
    SqlMasterUser = "$($prefix)_masteruser"
    SqlMasterPassword = $sqlSitecorePassword
    SqlWebUser = "$($prefix)_webuser"
    SqlWebPassword = $sqlSitecorePassword
    SqlReportingUser = "$($prefix)_reportinguser"
    SqlReportingPassword = $sqlSitecorePassword
    SqlReferenceDataUser = "$($prefix)_referencedatauser"
    SqlReferenceDataPassword = $sqlSitecorePassword
    SqlFormsUser = "$($prefix)_formsuser"
    SqlFormsPassword = $sqlSitecorePassword
    SqlExmMasterUser = "$($prefix)_exmmasteruser"
    SqlExmMasterPassword = $sqlSitecorePassword
    SqlMessagingUser = "$($prefix)_messaginguser"
    SqlMessagingPassword = $sqlSitecorePassword
    SqlServer = $sqlServer
    ExmEdsProvider = $edsProvider
    SolrUrl = $solrURL
    ProcessingService = $processingServiceURL
    ReportingService = $reportingServiceURL
    ReportingServiceApiKey = $reportingServiceApiKey
    XConnectCollectionSearchService = $xConnectCollectionSearchService
    XConnectReferenceDataService = $xConnectReferenceDataService
    MarketingAutomationOperationsService = $marketingAutomationOperationsService
    MarketingAutomationReportingService  = $marketingAutomationReportingService
    CortexReportingService = $xConnectCortexReportingService
    EXMCryptographicKey = $exmCryptographicKey
    EXMAuthenticationKey = $exmAuthenticationKey
    SitecoreIdentityAuthority = $identityServerUrl
    SitecoreIdentitySecret = $identityServerSecret
    TelerikEncryptionKey = $telerikEncryptionKey
    InstallDirectory = $installDir
}
Install-SitecoreConfiguration @sitecoreParams
AddWebBindings -SiteName $siteName -HostNames $hostNames
AddWebBindings -SiteName $siteName -HostNames $hostNames -SSLCert $serverCert
Write-Output "Install Sitecore CM done."