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 :)

      June 06, 2017

      Moto Vs Xiaomi

      This is more of a comparison based on personal experience between Motorola and Xiaomi phones.

      Having used Motorola phone for the past 2+ years (Moto G2 and Moto G Turbo), recently I made a switch to Xiaomi Redmi Note 4, and yes, the 64 GB/4GB version is what I am talking about.

      Although not very keen on changing the phone as I pretty much loved the Moto G Turbo, I ditched Moto for the hype of 4 GB RAM (My laptop has less RAM than this !) with a huge storage space and exciting price.

      While I say that the Redmi is a powerful phone (atleast on paper :)) and the build quality is good, still nothing can beat the simplicity and smoothness of the Moto.

      Lets compare the Redmi Note 4 with Moto G Turbo :

      Specs and Build Quality
      To simply put, build quality of both the phones is superb.
      On paper the Redmi Note 4 wins hands down on the specs from Moto G Turbo, with 5.5 inch screen vs 5 inch, 4GB Ram vs 2GB Ram, 4000mAh battery vs 2500mAh and so on.
      Redmi supports Fingerprint scanner while the Moto is IP67 certified making it water and dust proof.

      What Redmi wins on the hardware front, it kind of loses on the phone build and looks. With Moto G Turbo you get a superb back cover which really helps get a grip of the phone in the hand, on the other hand Redmi's back cover is super smooth and the phone is bound to slip from the hands making it difficult to use (unless you use a case cover). Moto also has added a subtle touch added on the power/wake button making it feel rugged and easier to recognize. So on the phone build and looks its Moto hands down.

      Software
      Redmi Note 4 comes with Android 6 (Marshmallow) with MIUI skin on top of Android. On the other hand Moto G Turbo comes with vanilla Android 6 (Marshmallow), upgradable to Android N. While MIUI has been praised for its ability to customize almost everything, vanilla android stands out for its simplicity, beauty, and common-sense. The vanilla android has the omni-present Google Bar where you can search almost anything. The Google Now integration on the Moto is also much smoother all its takes is a left swipe to view all the relevant information right at your fingertips. The MIUI skin also comes bloated with too many apps and softwares. Though I really liked MIUI for its three-finger swipe to take screenshot and the messaging app where it detects if OTP, and other codes and lets you copy it with a click.

      Camera
      Both the phones Camera quality is very good. The Note 4 camera app lets you have more control and comes with multiple options for filters, but its all software which can be achieved on the Moto G Turbo by installing the right app. The picture quality from both the phones is pretty much decent.

      Conclusion

      Redmi Note 4 is a powerful phone and there is no doubt about it. But it is also bloated and feels sluggish under heavy use through multiple apps. Make no mistake if there is only one app running it runs smoothly, but starting Whatsapp to read the messages, replying to an email, reading the news/weather updates, and listening to music !, well that takes a toll on the Redmi. With Moto on the other hand the experience is as smooth as butter, though some extremely high graphics game do take a toll.
      So its Moto for general, day-to-day use and with its super smooth Android experience, Moto integration apps, and the like its the best bet on buying a phone. And with latest Moto models having fingerprint scanner and the likes its seems hard to resist.
      Redmi does have its appreciation for its value-for-money, but until the software is fixed, I recommend only for intensive gamers and the likes.