Windows - Ensure Features Installed

Octopus.Script exported 2017-10-03 by WesleySSmith belongs to ‘Windows’ category.

Ensures that a set of Windows Features are installed on the system.

Parameters

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

Windows features

WindowsFeatures

The set of Windows Features that should be installed on the system. This can be either a single Windows Feature or a comma separated list of Windows Features to check.

Example 1: IIS-WebServer

Example 2: IIS-WebServer, IIS-WindowsAuthentication

Suppress Reboot

SuppressReboot = False

Suppresses reboot. If a reboot is not necessary, then this option does nothing. This option will keep DISM.exe from prompting for a restart, or from restarting automatically).

Script body

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

$requiredFeatures = $OctopusParameters['WindowsFeatures'].split(",") | foreach { $_.trim() }
if(! $requiredFeatures) {
    Write-Output "No required Windows Features specified..."
    exit
}
$requiredFeatures | foreach { $feature = DISM.exe /ONLINE /Get-FeatureInfo /FeatureName:$_; if($feature -like "*Feature name $_ is unknown*") { throw $feature } }

Write-Output "Retrieving all Windows Features..."
$allFeatures = DISM.exe /ONLINE /Get-Features /FORMAT:List | Where-Object { $_.StartsWith("Feature Name") -OR $_.StartsWith("State") } 
$features = new-object System.Collections.ArrayList
for($i = 0; $i -lt $allFeatures.length; $i=$i+2) {
    $feature = $allFeatures[$i]
    $state = $allFeatures[$i+1]
    $features.add(@{feature=$feature.split(":")[1].trim();state=$state.split(":")[1].trim()}) | OUT-NULL
}

Write-Output "Checking for missing Windows Features..."
$missingFeatures = new-object System.Collections.ArrayList
$features | foreach { if( $requiredFeatures -contains $_.feature -and $_.state.StartsWith("Disabled")) { $missingFeatures.add($_.feature) | OUT-NULL } }
if(! $missingFeatures) {
    Write-Output "All required Windows Features are installed"
    exit
}
Write-Output "Installing missing Windows Features..."
$featureNameArgs = ""
$missingFeatures | foreach { $featureNameArgs = $featureNameArgs + " /FeatureName:" + $_ }
$dism = "DISM.exe"
IF ($SuppressReboot)
{
    $arguments = "/NoRestart "
}
ELSE
{
    $arguments = ""
}
$arguments = $arguments + "/ONLINE /Enable-Feature /All $featureNameArgs"
Write-Output "Calling DISM with arguments: $arguments"
start-process -NoNewWindow $dism $arguments

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": "ed837372-165f-4e0e-b755-1df9633d9eb1",
  "Name": "Windows - Ensure Features Installed",
  "Description": "Ensures that a set of Windows Features are installed on the system.",
  "Version": 4,
  "ExportedAt": "2017-10-03T21:58:18.839Z",
  "ActionType": "Octopus.Script",
  "Author": "WesleySSmith",
  "Parameters": [
    {
      "Name": "WindowsFeatures",
      "Label": "Windows features",
      "HelpText": "The set of Windows Features that should be installed on the system. This can be either a single Windows Feature or a comma separated list of Windows Features to check.\n\nExample 1: IIS-WebServer\n\nExample 2: IIS-WebServer, IIS-WindowsAuthentication",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    },
    {
      "Name": "SuppressReboot",
      "Label": "Suppress Reboot",
      "HelpText": "Suppresses reboot. If a reboot is not necessary, then this option does nothing. This option will keep DISM.exe from prompting for a restart, or from restarting automatically).",
      "DefaultValue": "False",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$requiredFeatures = $OctopusParameters['WindowsFeatures'].split(\",\") | foreach { $_.trim() }\nif(! $requiredFeatures) {\n    Write-Output \"No required Windows Features specified...\"\n    exit\n}\n$requiredFeatures | foreach { $feature = DISM.exe /ONLINE /Get-FeatureInfo /FeatureName:$_; if($feature -like \"*Feature name $_ is unknown*\") { throw $feature } }\n\nWrite-Output \"Retrieving all Windows Features...\"\n$allFeatures = DISM.exe /ONLINE /Get-Features /FORMAT:List | Where-Object { $_.StartsWith(\"Feature Name\") -OR $_.StartsWith(\"State\") } \n$features = new-object System.Collections.ArrayList\nfor($i = 0; $i -lt $allFeatures.length; $i=$i+2) {\n    $feature = $allFeatures[$i]\n    $state = $allFeatures[$i+1]\n    $features.add(@{feature=$feature.split(\":\")[1].trim();state=$state.split(\":\")[1].trim()}) | OUT-NULL\n}\n\nWrite-Output \"Checking for missing Windows Features...\"\n$missingFeatures = new-object System.Collections.ArrayList\n$features | foreach { if( $requiredFeatures -contains $_.feature -and $_.state.StartsWith(\"Disabled\")) { $missingFeatures.add($_.feature) | OUT-NULL } }\nif(! $missingFeatures) {\n    Write-Output \"All required Windows Features are installed\"\n    exit\n}\nWrite-Output \"Installing missing Windows Features...\"\n$featureNameArgs = \"\"\n$missingFeatures | foreach { $featureNameArgs = $featureNameArgs + \" /FeatureName:\" + $_ }\n$dism = \"DISM.exe\"\nIF ($SuppressReboot)\n{\n    $arguments = \"/NoRestart \"\n}\nELSE\n{\n    $arguments = \"\"\n}\n$arguments = $arguments + \"/ONLINE /Enable-Feature /All $featureNameArgs\"\nWrite-Output \"Calling DISM with arguments: $arguments\"\nstart-process -NoNewWindow $dism $arguments",
    "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-features-installed.json",
  "Website": "/step-templates/ed837372-165f-4e0e-b755-1df9633d9eb1",
  "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 Tuesday, October 3, 2017