Execute AWS Powershell Script

Octopus.Script exported 2023-06-05 by pauby belongs to ‘AWS’ category.

This combines two previous library templates of checking for Chocolatey being installed, and installing something via Chocolatey, in this case awstools.powershell, and then adds on a third piece of running a custom Powershell script using AWS Powershell tools

Parameters

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

SecretKey

SecretKey

Enter your AWS Secret Key here. This will be used to authenticate the session with AWS

AccessKey

AccessKey

Enter your AWS Access Key here. This will be used to authenticate the session with AWS

Region

Region

Enter the region for where you will be executing your powershell scripts against. If you are unsure of the region you are in, check the list found here

AWSScript

AWSScript

This is the Powershell Script that contains commands using the AWSPowershell module that you want to execute

Script body

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

Write-Output "Ensuring the Chocolatey package manager is installed..."

$chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "Machine") + "\bin"
$chocolateyExe = "$chocolateyBin\choco.exe"
$chocInstalled = Test-Path $chocolateyExe

if (-not $chocInstalled) {
  Write-Output "Chocolatey not found, installing..."

  $installPs1 = ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
  Invoke-Expression $installPs1

  Write-Output "Chocolatey installation complete."
} else {
  Write-Output "Chocolatey was found at $chocolateyBin and won't be reinstalled."
}

$ChocolateyPackageId = 'awstools.powershell'

if (-not $ChocolateyPackageId) {
  throw "Please specify the ID of an application package to install."
}

if (-not $ChocolateyPackageVersion) {
  Write-Output "Installing package $ChocolateyPackageId from the Chocolatey package repository..."
  & $chocolateyExe install $ChocolateyPackageId
} else {
  Write-Output "Installing package $ChocolateyPackageId version $ChocolateyPackageVersion from the Chocolatey package repository..."
  & $chocolateyExe install $ChocolateyPackageId --version $ChocolateyPackageVersion
}

Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"

Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs AWSKeyProfile

Initialize-AWSDefaults -ProfileName AWSKeyProfile -Region $Region


Invoke-Expression $AWSScript

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": "551a6f13-9e4c-4d45-8387-f424d5ca02a4",
  "Name": "Execute AWS Powershell Script",
  "Description": "This combines two previous library templates of [checking for Chocolatey being installed](https://library.octopus.com//step-templates/c364b0a5-a0b7-48f8-a1a4-35e9f54a82d3/actiontemplate-chocolatey-ensure-installed), and [installing something via Chocolatey](https://library.octopus.com/step-templates/b2385b12-e5b5-440f-bed8-6598c29b2528/actiontemplate-chocolatey-install-package), in this case awstools.powershell, and then adds on a third piece of running a custom Powershell script using AWS Powershell tools",
  "Version": 8,
  "ExportedAt": "2023-06-05T12:27:05.077+00:00",
  "ActionType": "Octopus.Script",
  "Author": "pauby",
  "Parameters": [
    {
      "Name": "SecretKey",
      "Label": "SecretKey",
      "HelpText": "Enter your AWS Secret Key here. This will be used to authenticate the session with AWS",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "AccessKey",
      "Label": "AccessKey",
      "HelpText": "Enter your AWS Access Key here. This will be used to authenticate the session with AWS",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Region",
      "Label": "Region",
      "HelpText": "Enter the region for where you will be executing your powershell scripts against. If you are unsure of the region you are in, check the list found [here](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html)",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "AWSScript",
      "Label": "AWSScript",
      "HelpText": "This is the Powershell Script that contains commands using the AWSPowershell module that you want to execute",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "Write-Output \"Ensuring the Chocolatey package manager is installed...\"\n\n$chocolateyBin = [Environment]::GetEnvironmentVariable(\"ChocolateyInstall\", \"Machine\") + \"\\bin\"\n$chocolateyExe = \"$chocolateyBin\\choco.exe\"\n$chocInstalled = Test-Path $chocolateyExe\n\nif (-not $chocInstalled) {\n  Write-Output \"Chocolatey not found, installing...\"\n\n  $installPs1 = ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))\n  Invoke-Expression $installPs1\n\n  Write-Output \"Chocolatey installation complete.\"\n} else {\n  Write-Output \"Chocolatey was found at $chocolateyBin and won't be reinstalled.\"\n}\n\n$ChocolateyPackageId = 'awstools.powershell'\n\nif (-not $ChocolateyPackageId) {\n  throw \"Please specify the ID of an application package to install.\"\n}\n\nif (-not $ChocolateyPackageVersion) {\n  Write-Output \"Installing package $ChocolateyPackageId from the Chocolatey package repository...\"\n  & $chocolateyExe install $ChocolateyPackageId\n} else {\n  Write-Output \"Installing package $ChocolateyPackageId version $ChocolateyPackageVersion from the Chocolatey package repository...\"\n  & $chocolateyExe install $ChocolateyPackageId --version $ChocolateyPackageVersion\n}\n\nImport-Module \"C:\\Program Files (x86)\\AWS Tools\\PowerShell\\AWSPowerShell\\AWSPowerShell.psd1\"\n\nSet-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs AWSKeyProfile\n\nInitialize-AWSDefaults -ProfileName AWSKeyProfile -Region $Region\n\n\nInvoke-Expression $AWSScript",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-execute-powershell.json",
  "Website": "/step-templates/551a6f13-9e4c-4d45-8387-f424d5ca02a4",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////9o0R/eLD/Nu0/erS95Qg+bhr95sv/vHh+r96/vjw+bFc/NSl+KI++82W+saI+KpNeDqM1wAAA41JREFUeN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XhR/i1NqIte5pkUEEUQQQQQRRBBBBBFEEEEEEUR+g4jQUZBEqjqFO9mOiyeShoXvYoukZOG4GCLpWZgu83/vTNRidhlE0rYAAAAAAAAAAAAAAAAAAACAZPkjwAAMDi+bsnPP/wAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Monday, June 5, 2023