This is one of those blog posts that's as much for my personal future reference as it is for anyone else looking to do this.
At my place of work we run our web platform on Azure, and we make use of blob storage. To achieve this locally we use the Azure Storage Emulator.
I've always found it ever so slightly irritating that I have to manually start the Azure Storage Emulator, whereas Microsoft's implementation of Redis runs as a service which consequently means that its startup and shutdown are taken care of for me. Some time back I placed a shortcut to the StartStorageEmulator.cmd (found in C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator if you have the Azure Storage Emulator installed) into the Startup folder, which works fine but you get the command window flashing up when Windows starts, and it remains open until manually closed.
Being a software developer I naturally sought out a better, automated means of doing this 'silently', i.e. without the user seeing it happen.
And assuming you have the Azure Storage Emulator installed, here's how to do it…
In C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator create a new file - I called it "AndrewsStartScript.vbs" so that its purpose is clear to me if I stumble across it in future.
Place the following text into this new file:
Dim WinScriptHost Set WinScriptHost = CreateObject("WScript.Shell") WinScriptHost.Run Chr(34) & "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\StartStorageEmulator.cmd" & Chr(34), 0 Set WinScriptHost = Nothing
The WinScriptHost.Run method takes two parameters: the path of the script to run, and then an integer explaining the display properties of the window - in this case 0 which instructs Windows to not display the command window in which the script is being executed.
Now hit the Windows key (or click Start) and type "Task Scheduler" (without quotes), then hit <Enter> or click on the result to open said Task Scheduler.
In the Actions pane on the right-hand-side, click to "Create Task…". You should now be presented with the following.
Creating the scheduled task - step 1.
In this opening step, give the scheduled task a name (I've called it "Azure Storage Emulator" - I'd like to have added "Start" to this name but you can't edit it once created, and it's not that important that I'm going to re-create the task). Also, ensure that the checkbox labelled "Hidden" at the bottom-left is checked.
Now click on the second tab, Triggers, then click on the button labelled New….
Creating the scheduled task - step 2.
This will open a new window.
In the drop-down list at the top of this new window, select "At log on".
You can also change which user this scheduled task applies to, in case multiple people use this PC.
Creating the scheduled task - step 3.
Click OK to close this window where you defined the trigger for the task.
Now click on the third tab, Actions, and then click on the button labelled New….
Creating the scheduled task - step 4.
Once again, this will open a new window.
Creating the scheduled task - step 5.
This is where we define what will happen when this task is invoked.
- In the drop-down list at the top, select "Start a program".
- In the Program/script box, type "wscript.exe" (without quotes).
- In the Add arguments (optional) box, type "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\YOUR_FILE_NAME.vbs" (this time with quotes), replacing "YOUR_FILE_NAME" with the name you used for the file in step 1.
- In the Start in (optional) box, type "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator" (without quotes).
Click OK to close this window where you defined the action which will take place when the task is invoked.
Click on the fourth tab, Conditions.
You have several more options here but the only one which I changed was to ensure that the task runs regardless of whether I'm connected to a power supply.
Creating the scheduled task - step 6.
Click OK to close the window in which you defined the scheduled task.
And that's it. Next time you restart your computer you should see nothing until the Action Centre notification that the Azure Storage Emulator has started.
You may have noticed when shutting down your computer that the Azure Storage Emulator gives a warning on which you have to click OK.
To avoid this…
I created another script in C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator. I called this one StopStorageEmulator.cmd because it's pretty much a mirror of the provided StartStorageEmulator.cmd.
Here are the contents of StopStorageEmulator.cmd:
@echo off" C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" stop
I didn't create a scheduled task for this script, though, mostly because the task scheduler doesn't define an event for shutting down or logging off, which prevents us from scheduling tasks around such an event.
Hit the Windows key or click Start and type "Edit Group Policy" (without quotes), then hit <Enter> or click on the result.
In the left-hand pane, navigate to Computer Configuration > Windows Settings > Scripts (Startup/Shutdown).
Shutting down gracefully - step 1.
In the larger, right-hand pane, double-click Shutdown. A new window will appear labelled Shutdown Properties.
Click Add… and a new window labelled Add a Script will appear.
Shutting down gracefully - step 2.
Click Browse… adjacent to Script Name, then navigate to the file created in step 1 of this shutdown procedure.
Shutting down gracefully - step 3.
Click OK to close the Add a Script window, then OK again to close the Shutdown Properties window. You can also close the Local Group Policy Editor as well.
There you have it: a scheduled task which will silently run a startup script when you log in, then another script which will be run when the computer shuts down.