Coldfusion 10: Create ColdFusion instances from Windows PowerShell

If you need to install ColdFusion on Windows you may have the need to create instances by command line script. I have found some Linux bash examples in the Adobe forums, but nobody shared any script for Windows and these Linux versions cannot used. I had the need to write this scripts for Windows Core installation that is a bit more challenging than a GUI based installation, but the many benefits of a core installation out-weight the required work. I'm sharing these script here and hope these is of help for you getting these annoying tasks automated.

You need to prepare yourself first to use the script. As first step you need to get the cfadminPassword and this requires some debugging.

  1. Start up Developer Tools in Google Chrome (or Firefox Firebug).
  2. Change to Network tab.
  3. Enable Preserve log setting.
  4. Open your ColdFusion Administrator login page and login with your password.
  5. Go back to the Developer Tools and search the Network tab for enter.cfm request.
  6. Open the Headers of this request and scroll down to Form Data section.
  7. Copy the value of cfadminPassword and use this as value for $form['cfadminPassword']. This is the Your JS crypted CFadmin password.

This script creates additional ColdFusion instances and create the required Windows services. This script also implements adding a Logon as Service user, that is recommended per ColdFusion Server lock-down guide.

# Instances you'd like to create
$cfInstances = 'test1','test2','test3'
 
# Base path to ColdFusion
$cfBaseDir = 'C:\ColdFusion10'
 
$form = @{}
$form['cfadminUserId'] = 'admin'
$form['cfadminPassword'] = '[Your JS crypted CFadmin password]'
 
# Windows service
$service_account = '[Windows Service Username]'
$service_password = '[Windows Service Password]'
 
# Login and complete instance installation.
$r = Invoke-WebRequest -Uri "http://localhost:8500/CFIDE/administrator/index.cfm?configServer=true" -UseBasicParsing -SessionVariable 'cfAdminSession' -Method POST -Body $form -TimeoutSec 180
# Make sure installation completed.
$r = Invoke-WebRequest -Uri "http://localhost:8500/CFIDE/administrator/index.cfm?configServer=true" -UseBasicParsing -WebSession $cfAdminSession
 
# Login to coldfusion administrator
$EnterpriseManagerUrl = 'http://localhost:8500/CFIDE/administrator/entman'
$r = Invoke-WebRequest -Uri "http://localhost:8500/CFIDE/administrator/enter.cfm" -UseBasicParsing -WebSession $cfAdminSession -Method POST -Body $form
$r = Invoke-WebRequest -Uri "$EnterpriseManagerUrl/index.cfm" -UseBasicParsing -WebSession $cfAdminSession
$r.Content -cmatch '.*action="(addserver.cfm\?servertype=addlocal[^"]*).*'
$r = Invoke-WebRequest -Uri "$EnterpriseManagerUrl/$($matches[1])" -UseBasicParsing -WebSession $cfAdminSession
$r.Content -cmatch '.*action="(processaddserver.cfm?[^"]*).*'
 
# Create the instances.
Foreach ($cfInstance in $cfInstances) {
  $form['serverName'] = $cfInstance
  $form['directory'] = "C:\ColdFusion10\$cfInstance"
  # Required: 'cfusion' instance user need Administrator permissions on the local machine or the service cannot created automatically.
  $form['windows_svc'] = 'on'
  $r = Invoke-WebRequest -Uri "$EnterpriseManagerUrl/$($matches[1])" -UseBasicParsing -WebSession $cfAdminSession -Method POST -Body $form -TimeoutSec 180
 
  # FusionReactor argument may be copied from existing 'cfusion' instance, remove it. FR will be configured later! Otherwise CF service will not start.
  $jvm_config = "$cfBaseDir\$cfInstance\bin\jvm.config"
  if ((Test-Path -path $jvm_config)) {
    $content = Get-Content $jvm_config
    if ($content -match '-javaagent:C:/FusionReactor/(\w+)/fusionreactor.jar=address=(\d{4})\s') {
      $content -replace '-javaagent:C:/FusionReactor/(\w+)/fusionreactor.jar=address=(\d{4})\s','' | Set-Content $jvm_config
      Write-Host "$cfInstance`: Broken FusionReactor argument has been removed."
    }
  }
 
  # Change the "Logon as" user
  $svc = gwmi win32_service -filter "name='ColdFusion 10 Application Server $cfInstance'"
  $r = $svc.StopService()
  # ReturnValues: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384901%28v=vs.85%29.aspx
  $status = $svc.change($null,$null,$null,$null,$null,$null,$service_account,$service_password).ReturnValue
  if ($status -eq 0) {
    # 0 = The request was accepted.
    Write-Host "$cfInstance`: Logon as Service configured successful."
  }
  else {
    Write-Host "$cfInstance`: Logon as Service configuration failed with ReturnCode $status."
  }
  $r = $svc.StartService()
}

Requirements: Powershell.

Rating
Average: 8.4 (8 votes)