Assign PowerShell script to run at startup using PowerShell on Window Server 2012
I'm trying to write a PowerShell script that will run when a Windows 2012 instance is created on AWS using the configuration tools provided by AWS.
My problem is that I want to change the name of the machine once it has started up, restart the machine and carry on set up process after. The main reason for this is that one of the applications, Boundary, installed in the set up process takes the name of the server when first installed. It is then doesn't seem possible to change it's name in their portal.
Ideally I would have two PowerShell scripts, one to start the set up process, initialised through AWS and another that runs the first time the machine restarts. This second script would ideally be queued to run on the next start by the initial set up script. So I guess my question are:
- Is this possible?
- How would I go about doing this.
My Google foo is letting me down here so any answers would be appreciated.
It is definitely possible. The basic building blocks you will need are:
- Amazon's 'User Data' feature, which enables running a user defined script when the instance first launches. There are more details here: https://forums.aws.amazon.com/message.jspa?messageID=342503 .
- Windows Task Scheduler to schedule a task to execute after Windows restarts. Look at the schtasks.exe command for more details on how to do this (Just type schtasks.exe at the command prompt).
You will need two scripts:
-
Runs via the 'User Data' feature. Responsible for scheduling the execution of the second script using the windows task scheduler on Windows start up, changing the Windows machine name and restarting Windows. The scheduling command should look something like:
schtasks.exe /create /TN my-task /SC ONSTART /TR SCRIPT_FILE /RU USERNAME /RP PASSWORD
-
Runs your applications.
The second script may also delete the scheduled task - depends if you want this VM to recover automatically from restarts or not.