Registry - Update Configuration Variables In Export File

Octopus.Script exported 2015-07-02 by jbennett belongs to ‘Windows’ category.

Replace values in a .reg file (Registry Export) automatically with octopus variables.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Registry Exports

regExports

A list of .reg files (Registry exports) separated by a ;

Script body

Steps based on this template will execute the following PowerShell script.

# Running outside octopus
param(
    [string]$regExports
) 

$ErrorActionPreference = "Stop" 

function Get-Param($Name, [switch]$Required, $Default) {
    $result = $null

    if ($OctopusParameters -ne $null) {
        $result = $OctopusParameters[$Name]
    }

    if ($result -eq $null) {
        $variable = Get-Variable $Name -EA SilentlyContinue   
        if ($variable -ne $null) {
            $result = $variable.Value
        }
    }

    if ($result -eq $null) {
        if ($Required) {
            throw "Missing parameter value $Name"
        } else {
            $result = $Default
        }
    }

    return $result
}

# More custom functions would go here

& {
    param(
        [string]$regExports
    ) 

    Write-Host "Registry Export Configuration Variables"
    Write-Host "regExports: $regExports"

    ForEach ($regExp in $regExports.Split(';'))  {
        
        $regFile = $regExp.Trim()
        
        if( $regFile.Length -lt 1 ){ break }

        $output = ""

        $fi=Get-Item $regFile
        $file=$fi.OpenText()

        While(!($file.EndOfStream)){
            $line=$file.ReadLine()
            $outputLine = $line

            if($line -match "`"=`""){
                $keyValue = $line -split "`"=`""
                $key = $keyValue[0] -replace "^`"" , ""
                $oldVal = $keyValue[1] -replace "`"$" , ""
                $newVal = $OctopusParameters[$key]
                
                Write-Host "Looking for key $key in OctopusParameters hash"

                if($newVal){
                    Write-Host "Updating $key from $oldVal to $newVal"
                    $outputLine = "`"$key`"=`"$newVal`""
                }
            }
            
            $output += $outputLine + "`r`n"
        }

        $output | Out-File "c:\temp\output.reg"
    }

 } `
(Get-Param 'regExports' -Required)

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "7f9c1f6d-8305-4a37-a4c6-9b47ea4699d9",
  "Name": "Registry - Update Configuration Variables In Export File",
  "Description": "Replace values in a .reg file (Registry Export) automatically with octopus variables.",
  "Version": 2,
  "ExportedAt": "2015-07-02T14:59:00.322+00:00",
  "ActionType": "Octopus.Script",
  "Author": "jbennett",
  "Parameters": [
    {
      "Name": "regExports",
      "Label": "Registry Exports",
      "HelpText": "A list of .reg files (Registry exports) separated by a ;",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Running outside octopus\nparam(\n    [string]$regExports\n) \n\n$ErrorActionPreference = \"Stop\" \n\nfunction Get-Param($Name, [switch]$Required, $Default) {\n    $result = $null\n\n    if ($OctopusParameters -ne $null) {\n        $result = $OctopusParameters[$Name]\n    }\n\n    if ($result -eq $null) {\n        $variable = Get-Variable $Name -EA SilentlyContinue   \n        if ($variable -ne $null) {\n            $result = $variable.Value\n        }\n    }\n\n    if ($result -eq $null) {\n        if ($Required) {\n            throw \"Missing parameter value $Name\"\n        } else {\n            $result = $Default\n        }\n    }\n\n    return $result\n}\n\n# More custom functions would go here\n\n& {\n    param(\n        [string]$regExports\n    ) \n\n    Write-Host \"Registry Export Configuration Variables\"\n    Write-Host \"regExports: $regExports\"\n\n    ForEach ($regExp in $regExports.Split(';'))  {\n        \n        $regFile = $regExp.Trim()\n        \n        if( $regFile.Length -lt 1 ){ break }\n\n        $output = \"\"\n\n        $fi=Get-Item $regFile\n        $file=$fi.OpenText()\n\n        While(!($file.EndOfStream)){\n            $line=$file.ReadLine()\n            $outputLine = $line\n\n            if($line -match \"`\"=`\"\"){\n                $keyValue = $line -split \"`\"=`\"\"\n                $key = $keyValue[0] -replace \"^`\"\" , \"\"\n                $oldVal = $keyValue[1] -replace \"`\"$\" , \"\"\n                $newVal = $OctopusParameters[$key]\n                \n                Write-Host \"Looking for key $key in OctopusParameters hash\"\n\n                if($newVal){\n                    Write-Host \"Updating $key from $oldVal to $newVal\"\n                    $outputLine = \"`\"$key`\"=`\"$newVal`\"\"\n                }\n            }\n            \n            $output += $outputLine + \"`r`n\"\n        }\n\n        $output | Out-File \"c:\\temp\\output.reg\"\n    }\n\n } `\n(Get-Param 'regExports' -Required)",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/registry-update-configuration-variables-in-export-file.json",
  "Website": "/step-templates/7f9c1f6d-8305-4a37-a4c6-9b47ea4699d9",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, July 2, 2015