thinking in rules

Basic irCatalog Management using Powershell

Josh Elster | 1/23/2017

Hand-modification of a web.config file is one of the more common post-installation tasks that our customers find themselves performing. Similarly, because I am always setting up and tearing down new environments and servers, I find myself repeatedly needing to perform some common tasks.

The typical use case for this script is when a catalog database has been replaced or restored to a different server, necessitating an administrator update to the connection string under the inrule.repository.service configuration node.

Because I'm a lazy developer, I decided to write a script to avoid the repetition. Since I'm also prone to mix up or miss steps and typo config entries, making the steps involved in configuring a new environment reliably repeatable is a huge benefit as well.

The script (see below for full text), Set-CatalogConfig.ps1, will use the provided parameters to construct a connection string for the given catalog database on the given database host. Then, it loads the repository service's web.config (defaults to the same as the default installation path) and navigates down to the inrule.repository.service.connectionString node before setting the value to the constructed connection string and saving the updated config.

You may wonder why I'm going to the trouble of loading the XML and navigating it via the XML object model. The reason is that having the object loaded makes it trivial to add additional configuration settings management abilities.

Note: the ' around the names of the InRule custom configuration sections escapes what wouldn't otherwise be treated as a single node.

This script does only a little bit of error checking and uses some hard-coded values - Your Mileage May Vary. The script demonstrates support for the WhatIf flag as well as writing additional messages when the -Verbose flag is provided to aid in troubleshooting and less risky experimentation.

In my next post on this topic, I'll talk about a script that creates the database and schema for irCatalog. In the meantime, I hope you find this useful!

 
[CmdletBinding()]
param(
    [string]$catalogDbHostName = "localhost",
    [string]$catalogUserName,
    [string]$catalogPassword= "_", # TODO: use SecureString
    [string]$catalogInstallPath = "C:\Program Files (x86)\InRule\irServer\RepositoryService\IisService",
    [string]$catalogDbName = "InRuleCatalog"
    )
    
$ErrorActionPreference = 'Stop'

if ($catalogPassword -eq "_" -or $catalogPassword -eq $null) {
    Write-Verbose 'Invalid DB connection string information supplied to setup script. Ensure the catalogUserName and catalogPassword parameters are suppleid. NOTE: Integration Windows Authentication is not currently supported by this script.'
    return -1
}

if ((test-path $catalogInstallPath) -eq $false) {
    Write-Verbose "cannot find path to irCatalog configuration file.";
    return -1;
}
$catConString = "Server=tcp:$catalogDbHostName,1433;User ID=$catalogUserName;Password=$catalogPassword;Initial Catalog=$catalogDbName;"
write-Verbose "Replacing supplied configuration values in web.config..."
$configFilePath = resolve-Path -Path (Join-Path -Path $catalogInstallPath -ChildPath "Web.config")

$cfgXml = new-object Xml
$cfgXml.Load($configFilePath)
$cstring = $cfgXml.configuration.'inrule.repository.service'.connectionString
if ($cstring -eq $null) {
    write-error "Could not locate configuration section inrule.repository.service.connectionString. irCatalog is unlikely to function in this state."
    $cstring = $cfgXml.CreateElement("connectionString")
    
    $cfgXml.configuration.'inrule.repository.service'.appendChild($cstring)
    write-verbose "Added connectionString node to 'inrule.repository.service'"
}
write-verbose "Replacing constring $cstring with $catConString"

$cfgXml.configuration.'inrule.repository.service'.connectionString = $catConString
if ($PSCmdlet.ShouldProcess("Save configuration string changes to config")) {
    $cfgXml.Save($configFilePath)
}
write-Verbose "Saved configuration changes."
return 0;

comments powered by Disqus