Windows - Ensure Hosts File Entry Exists

Octopus.Script exported 2015-07-15 by bobjwalker belongs to ‘Windows’ category.

Ensures that the given IP/host name entry exists in the hosts file.

Parameters

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

IP Address

IP

The IP address which the host name should resolve to, e.g. 127.0.0.1

Host Name

HostName

The host name which should resolve to the given IP, e.g. www.mydomain.com

Script body

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

$ip = $OctopusParameters['IP']
$hostName = $OctopusParameters['HostName']

$hostsPath = "$env:windir\System32\drivers\etc\hosts"

$hosts = Get-Content $hostsPath

$match = $hosts -match ("^\s*$ip\s+$hostName" -replace '\.', '\.')

If ($match) { Exit }

$hostsEntry = "$ip`t$hostName"

If ([IO.File]::ReadAllText($hostsPath) -notmatch "\r\n\z") { $hostsEntry = [environment]::newline + $hostsEntry }

Add-Content $hostsPath $hostsEntry

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": "36e828dc-8679-451d-9f3d-6edf3e90babf",
  "Name": "Windows - Ensure Hosts File Entry Exists",
  "Description": "Ensures that the given IP/host name entry exists in the hosts file.",
  "Version": 6,
  "ExportedAt": "2015-07-15T15:02:15.749+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "IP",
      "Label": "IP Address",
      "HelpText": "The IP address which the host name should resolve to, e.g. 127.0.0.1",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "HostName",
      "Label": "Host Name",
      "HelpText": "The host name which should resolve to the given IP, e.g. www.mydomain.com",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$ip = $OctopusParameters['IP']\n$hostName = $OctopusParameters['HostName']\n\n$hostsPath = \"$env:windir\\System32\\drivers\\etc\\hosts\"\n\n$hosts = Get-Content $hostsPath\n\n$match = $hosts -match (\"^\\s*$ip\\s+$hostName\" -replace '\\.', '\\.')\n\nIf ($match) { Exit }\n\n$hostsEntry = \"$ip`t$hostName\"\n\nIf ([IO.File]::ReadAllText($hostsPath) -notmatch \"\\r\\n\\z\") { $hostsEntry = [environment]::newline + $hostsEntry }\n\nAdd-Content $hostsPath $hostsEntry\n",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-ensure-hosts-file-entry-exists.json",
  "Website": "/step-templates/36e828dc-8679-451d-9f3d-6edf3e90babf",
  "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 Wednesday, July 15, 2015