Windows - Apply Windows Updates

Octopus.Script exported 07/22/2020 by twerthi belongs to 'Windows' category.

Step template to check for and apply Windows Updates with optional automatic reboot.

Parameters

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

Auto reboot

windowsUpdateAutoReboot = False

Check the box to allow an automatic reboot. Warning: using this option will cause the machine to reboot after installing the first update that requires a reboot. If there are multiple updates that require a reboot, the rest of the updates will not be installed.

Script body

Steps based on this template will execute the following PowerShell script.
Show script
function Get-NugetPackageProviderNotInstalled
{
	# See if the nuget package provider has been installed
    return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))
}

function Get-ModuleInstalled
{
    # Define parameters
    param(
        $PowerShellModuleName
    )

    # Check to see if the module is installed
    if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))
    {
        # It is installed
        return $true
    }
    else
    {
        # Module not installed
        return $false
    }
}


# Force use of TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$autoReboot = [System.Convert]::ToBoolean("$windowsUpdateAutoReboot")

# Check to see if the NuGet package provider is installed
if ((Get-NugetPackageProviderNotInstalled) -ne $false)
{
  # Display that we need the nuget package provider
  Write-Host "Nuget package provider not found, installing ..."

  # Install Nuget package provider
  Install-PackageProvider -Name Nuget -Force

  Write-Output "Nuget package provider succesfully installed ..."
}


Write-Output "Checking for PowerShell module PSWindowsUpdate ..."

if ((Get-ModuleInstalled -PowerShellModuleName "PSWindowsUpdate") -ne $true)
{
	Write-Output "PSWindowsUpdate not found, installing ..."
    
    # Install PSWindowsUpdate
    Install-Module PSWindowsUpdate -Force
    
    Write-Output "Installation of PSWindowsUpdate complete ..."
}

Write-Output "Checking for updates ..."

$windowsUpdates = Get-WindowsUpdate 

# Check to see if there's anything to install
if ($windowsUpdates.Count -gt 0)
{
	Write-Output "Installing updates ..."
    if ($autoReboot)
    {
		Install-WindowsUpdate -AcceptAll -AutoReboot
    }
    else
    {
    	Install-WindowsUpdate -AcceptAll
    }
}
else
{
	Write-Output "There are no updates available."
}

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

Show JSON
{
  "Id": "3472f207-3934-44db-a4ac-1390167cf7ed",
  "Name": "Windows - Apply Windows Updates",
  "Description": "Step template to check for and apply Windows Updates with optional automatic reboot.",
  "Version": 1,
  "ExportedAt": "2020-07-22T23:47:53.859Z",
  "ActionType": "Octopus.Script",
  "Author": "twerthi",
  "Packages": [],
  "Parameters": [
    {
      "Id": "004de404-ec52-47d3-ad49-ea96224182c6",
      "Name": "windowsUpdateAutoReboot",
      "Label": "Auto reboot",
      "HelpText": "Check the box to allow an automatic reboot. **Warning**: using this option will cause the machine to reboot after installing the first update that requires a reboot.  If there are multiple updates that require a reboot, the rest of the updates will not be installed.",
      "DefaultValue": "False",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "function Get-NugetPackageProviderNotInstalled\n{\n\t# See if the nuget package provider has been installed\n    return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\nfunction Get-ModuleInstalled\n{\n    # Define parameters\n    param(\n        $PowerShellModuleName\n    )\n\n    # Check to see if the module is installed\n    if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))\n    {\n        # It is installed\n        return $true\n    }\n    else\n    {\n        # Module not installed\n        return $false\n    }\n}\n\n\n# Force use of TLS 1.2\n[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n\n$autoReboot = [System.Convert]::ToBoolean(\"$windowsUpdateAutoReboot\")\n\n# Check to see if the NuGet package provider is installed\nif ((Get-NugetPackageProviderNotInstalled) -ne $false)\n{\n  # Display that we need the nuget package provider\n  Write-Host \"Nuget package provider not found, installing ...\"\n\n  # Install Nuget package provider\n  Install-PackageProvider -Name Nuget -Force\n\n  Write-Output \"Nuget package provider succesfully installed ...\"\n}\n\n\nWrite-Output \"Checking for PowerShell module PSWindowsUpdate ...\"\n\nif ((Get-ModuleInstalled -PowerShellModuleName \"PSWindowsUpdate\") -ne $true)\n{\n\tWrite-Output \"PSWindowsUpdate not found, installing ...\"\n    \n    # Install PSWindowsUpdate\n    Install-Module PSWindowsUpdate -Force\n    \n    Write-Output \"Installation of PSWindowsUpdate complete ...\"\n}\n\nWrite-Output \"Checking for updates ...\"\n\n$windowsUpdates = Get-WindowsUpdate \n\n# Check to see if there's anything to install\nif ($windowsUpdates.Count -gt 0)\n{\n\tWrite-Output \"Installing updates ...\"\n    if ($autoReboot)\n    {\n\t\tInstall-WindowsUpdate -AcceptAll -AutoReboot\n    }\n    else\n    {\n    \tInstall-WindowsUpdate -AcceptAll\n    }\n}\nelse\n{\n\tWrite-Output \"There are no updates available.\"\n}",
    "Octopus.Action.EnabledFeatures": ""
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-apply-windows-updates.json",
  "Website": "/step-templates/3472f207-3934-44db-a4ac-1390167cf7ed",
  "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 »