Use PowerShell for Configuration and Activation

You can silently configure and activate Coveo for Sitecore using Swagger (see Activate silently).

In this article, you will learn how to use a PowerShell script that does this for you, which can be useful when automating package installation or upgrade.

Configuring Coveo for Sitecore

This function configures Coveo for Sitecore by applying the passed parameters values on the corresponding configuration files. It can be called either to fully or partially configure the instance.

Note

As long as the configuration files have the .config.example extension, Coveo for Sitecore won’t be activated (see Activating Coveo for Sitecore).

function Configure-CoveoForSitecorePackage([Parameter(Mandatory=$true)]
                                           [string] $SitecoreInstanceUrl,
                                           [string] $CoveoForSitecoreApiVersion = "v1",
                                           [string] $OrganizationId,
                                           [string] $ConfigApiKey,
                                           [string] $SearchApiKey,
                                           [boolean] $DisableSourceCreation = $false,
                                           [string] $CoveoSitecoreUsername,
                                           [string] $CoveoSitecorePassword,
                                           [string] $DocumentOptionsBodyIndexing = "Rich",
                                           [boolean] $DocumentOptionsIndexPermissions = $true,
                                           [string] $FarmName,
                                           [boolean] $BypassCoveoForSitecoreProxy = $false,
                                           [Parameter(Mandatory=$true)]
                                           [string] $ScriptSitecoreUsername,
                                           [Parameter(Mandatory=$true)]
                                           [string] $ScriptSitecorePassword,
                                           [string] $GeneralPlatformEndpointUrl = "",
                                           [string] $PlatformEndpointUrl = "",
                                           [string] $IndexingEndpointUrl = "",
                                           [string] $SearchEndpointUrl = "",
                                           [string] $UsageAnalyticsEndpointUrl = "")
{
    $ConfigureCoveoForSitecoreUrl = $SitecoreInstanceUrl + "/coveo/api/config/" + $CoveoForSitecoreApiVersion + "/configure"

    $Body = @{ }

    if (![string]::IsNullOrEmpty($OrganizationId)) {
        $Body.Organization = @{
            "OrganizationId" = $OrganizationId
            "ApiKey" = $ConfigApiKey
            "SearchApiKey" = $SearchApiKey
            "GeneralPlatformEndpointUrl" = $GeneralPlatformEndpointUrl
            "PlatformEndpointUrl" = $PlatformEndpointUrl
            "IndexingEndpointUrl" = $IndexingEndpointUrl
            "SearchEndpointUrl" = $SearchEndpointUrl
            "UsageAnalyticsEndpointUrl" = $UsageAnalyticsEndpointUrl
            "DisableSourceCreation" = $DisableSourceCreation
        }
        $Body.SitecoreCredentials = @{
            "Username" = $CoveoSitecoreUsername
            "Password" = $CoveoSitecorePassword
        }
        $Body.DocumentOptions = @{
            "BodyIndexing" = $DocumentOptionsBodyIndexing
            "IndexPermissions" = $DocumentOptionsIndexPermissions
        }
        $Body.Proxy = @{
            "BypassCoveoForSitecoreProxy" = $BypassCoveoForSitecoreProxy
        }
    }

    if (![string]::IsNullOrEmpty($FarmName)) {
        $Body.Farm = @{
            "Name" = $FarmName
        }
    }

    $BodyJson = $Body | ConvertTo-Json

    $m_Headers = @{
        "x-scUsername" = $ScriptSitecoreUsername
        "x-scPassword" = $ScriptSitecorePassword
    }

    Write-Host "Configuring the Coveo for Sitecore package... "
    Try {
        Invoke-RestMethod -Uri $ConfigureCoveoForSitecoreUrl -Method PUT -Body $BodyJson -Headers $m_Headers -ContentType "application/json"
        Write-Host "The Coveo for Sitecore package is now configured."
    }
    Catch {
        Write-Host "There was an error during your Coveo for Sitecore package configuration:"
        Write-Host $PSItem
    }
}

Activating Coveo for Sitecore

This function activates Coveo for Sitecore by renaming the *.config.example files to *.config. Since this is a configuration change, it will restart your IIS application pool once the operation is completed.

function Activate-CoveoForSitecorePackage([Parameter(Mandatory=$true)]
                                          [string] $SitecoreInstanceUrl,
                                          [string] $CoveoForSitecoreApiVersion = "v1",
                                          [Parameter(Mandatory=$true)]
                                          [string] $ScriptSitecoreUsername,
                                          [Parameter(Mandatory=$true)]
                                          [string] $ScriptSitecorePassword)
{
    $ActivateCoveoForSitecoreUrl = $SitecoreInstanceUrl + "/coveo/api/config/" + $CoveoForSitecoreApiVersion + "/activate"

    $m_Headers = @{
        "x-scUsername" = $ScriptSitecoreUsername
        "x-scPassword" = $ScriptSitecorePassword
    }

    Write-Host "Activating the Coveo for Sitecore package... "

    Try {
        Invoke-RestMethod -Uri $ActivateCoveoForSitecoreUrl -Method POST -Headers $m_Headers -ContentType "application/json"
        Write-Host "The Coveo for Sitecore package is now activated."
    }
    Catch {
        Write-Host "There was an error during your Coveo for Sitecore package activation:"
        Write-Host $PSItem
    }
}

Usage Example

Once these functions are added to your PowerShell script tooling, you can use them to configure and activate Coveo for Sitecore. Here is an example of how to call them with the right parameters:

. .\yourpowershellscriptfile.ps1
Configure-CoveoForSitecorePackage -SitecoreInstanceUrl <SITECORE_URL> -OrganizationId <ORGANIZATION_ID> -ConfigApiKey <API_KEY> -SearchApiKey <SEARCH_API_KEY> -CoveoSitecoreUsername <COVEO_USERNAME> -CoveoSitecorePassword <COVEO_PASSWORD> -ScriptSitecoreUsername <SCRIPT_USERNAME> -ScriptSitecorePassword <SCRIPT_PASSWORD>

. .\yourpowershellscriptfile.ps1
Activate-CoveoForSitecorePackage -SitecoreInstanceUrl <SITECORE_URL> -ScriptSitecoreUsername <SCRIPT_USERNAME> -ScriptSitecorePassword <SCRIPT_PASSWORD>

You need to replace:

  • <ORGANIZATION_ID> with the ID of your Coveo organization.

  • <API_KEY> with an unencrypted API key with the appropriate rights (see Activate silently).

  • <SEARCH_API_KEY> with an unencrypted API key with the appropriate rights (see Activate silently).

  • <COVEO_USERNAME> and <COVEO_PASSWORD> with the unencrypted credentials of a user that can read all the files that you want to index.

  • <SCRIPT_USERNAME> and <SCRIPT_PASSWORD> with the unencrypted credentials of a user that can modify the configuration files of your Sitecore instance.

    Tip

    The default credentials in Sitecore are sitecore\admin for the username and b for its password.

  • <SITECORE_URL> with your Sitecore instance URL.

Important

Azure’s Web Application Firewall (WAF) may block Coveo query network calls that trigger OWASP rules. You might need to disable or customize Azure WAF rules to allow Coveo for Sitecore network calls (see Azure Web Application Firewall triggered rules block Coveo for Sitecore’s communication on Content Delivery servers).