Powershell user data script run on restart of the Windows instance

amazon-web-servicesautoscalingcloud-initpowershellwindows-server-2016

We have windows servers that do application initialization which takes almost 3-4 hours for servers to be online. So to avoid downtime we use autoscaling group lifecycle hooks. Now we want to shut down all the servers at night and start them in the morning for cost optimization. Is it possible to run the PowerShell user-data script on the restart of the server? I followed link here however, cloud-init does not support Windows PowerShell user-data format as per this link.

Any suggestions would be a great help. Thank you.

Best Answer

I believe there is a way to run a PowerShell script from user data, however user-data script is only run on the first instance boot, not on every restart. At least that’s how it is in Linux, I guess on Windows it would be similar. Which means user-data / cloud-init script won’t do what you want anyway.

You should better create a job that runs every time the instance starts like it’s documented here: Use PowerShell to Create Job that Runs at Startup

Hope that helps :)