Can AWS CodeDeploy execute powershell scripts?
YES!
Although I haven't been able to find a definitive list of the acceptable script types, it looks like the answer is Yes
- Powershell .ps1
scripts are acceptable and will be executed if included in the appspec.yml
file.
My Powershell script wasn't working consistently until I added the code as recommended on the troubleshooting page by @kafka, so my script now contains the following above it:
# Are you running in 32-bit mode?
# (\SysWOW64\ = 32-bit mode)
if ($PSHOME -like "*SysWOW64*")
{
Write-Warning "Restarting this script under 64-bit Windows PowerShell."
# Restart this script under 64-bit Windows PowerShell.
# (\SysNative\ redirects to \System32\ for 64-bit mode)
& (Join-Path ($PSHOME -replace "SysWOW64", "SysNative") powershell.exe) -File `
(Join-Path $PSScriptRoot $MyInvocation.MyCommand) @args
# Exit 32-bit script.
Exit $LastExitCode
}
# Was restart successful?
Write-Warning "Hello from $PSHOME"
Write-Warning " (\SysWOW64\ = 32-bit mode, \System32\ = 64-bit mode)"
Write-Warning "Original arguments (if any): $args"
# Your 64-bit script code follows here...
# ...
#
# I PUT MY SCRIPT HERE
#
I am still unsure as to whether my script is only compatible with the 64 bit version of Powershell or how to find it out, but it works with this modification.
I hope that this helps someone.
Update: A note on file location
I'd like to highlight an issue that I faced with running .ps1
scripts. From my experience, ps1
scripts must be placed at the root of your deployment package (the same folder location as your appspec.yml file), if not, your script may fail to execute and the deployment will display as 'Successful' in CodeDeploy. More info on this here.
Yes it can but unless you set a variable errors encountered during running of the script will not stop your deployment.
$ErrorActionPreference = ‘Stop’
See this link for more details:
https://aws.amazon.com/premiumsupport/knowledge-center/powershell-cmdlet-errors-codedeploy/