June 29, 2017

[Windows]: Executing a script on Windows startup

If anyone has used Linux and have been working on how to have a script executed at machine startup, that person will tell you how simple it is to achieve it.
Having written this myself for a Linux based server where the script performed startup of all required components for my project and did the necessary initialization sequence and other stuff.
Along came a client who had a Windows 2012 server and I was tasked with replicating the same as the Linux box on Windows. So I started writing a Powershell script to do the startup sequence of my project. Well, it was not as smooth and simple task (as was with Linux) to write a similar Powershell script but finally after a few brain damaging moments (pun intended) I finally managed to complete the script.

So only the final step pending was to have the script execute on machine boot. Well, this step sounded really simple until it hit me hard and after literally banging my head against the walls
I finally managed to complete it *correctly*. While there are blogs/tutorials giving a guide for the same it was really useless because I wasn't getting my script to work. After much research and some more head-bangs-against-the-wall the below steps finally worked :
  1. Press the Windows key and type in "Task Scheduler"
  2. Once the result is found right-click the entry and click "Run as administrator". If it is not started as Administrator, the script which we are about to specify will not run correctly. (This was one of the head-bang moment)
  3. Once the application opens, click "Create Task.." as shown in the image
  4. Fill in the details :
    • Give Name and Description as appropriate
    • Select the User Account with which it should run
    • Select "Run whether user is logged on or not."
    • Check Run with highest privileges.
    • Check Hidden
    • Provide "Configure for" as the current OS version which this will run on.
  5. Goto the Trigger Tab
    • Click New
    • Specify the necessary Trigger details as appropriate
      • For executing on machine reboot select "At Startup"
      • Click Ok
  6. Goto Action Tab
    • Click New
    • For the Action select "Start a program"
    • Since the Task Scheduler will run the script as a normal batch script and not a powershell script. To execute a powershell script in the Program/Script specify the path to powershell executable. 
    • This is typically "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    • Add the arguments as "-executionpolicy bypass .\{{script-filename}}.ps1 > {{output-log}}.txt 2>&1"
    • Add "Start In" as the directory where the script is located
    • Now the arguments deserves some explanation :
      • Execution Policy specifies how/if should the execution begin. This is more related to using Powershell and the way Windows System handles execution. This is way too complex and you can read more on this here if you want (or go the simple route of specifying it as bypass).
      • Next is the script file name but notice we are giving relative path. You can also optionally give absolute path.
      • Next is the redirection of the output and/or we also redirect the errors to the same output
      • Note : here that it is different from the other guides as we do not specify the "-file" option. This was the head-band-multiple-times-against-the-wall moment because that is when I realized that giving the -file options (among the many other issues also due to this) is the reason why the redirection of the output and errors was not working.
    • Click Ok
  7. Click on the Conditions Tab
    • Change the settings as appropriate 
  8. Click on the Setting Tab
    • Change the settings as appropriate 
  9. Click OK
  10. And we are done :)
The output logs, error logs really helped me iron out the few issues that still were present in the script. But to get that working was a real strange mystery (and finally realizing the "-file" option as the culprit was a bummer).

Thanks and have a good day :)

      No comments:

      Post a Comment