Archive for July 3rd, 2011

Automatic NAS Box shutdown – How to execute applications on Windows Shutdown (Part 2/2)

In Part 1 we looked at how to figure out what message was being sent from the web management page of my Netgear ReadyNAS Duo network storage box, and then how to replicate that to happy from a c# dot net console application. To finish this off, we now need some way of making that new console app run automatically when I shut down my Windows 7 PC.

I started in the Task Scheduler panel in Control Panel > Administrative Tools.

On the right hand panel you can then select “Create Task…”. From this dialogue you have a number of tabs along the top- the second one along, right after General is “Triggers” – switch to this and then hit the “New…” button which brings up a third window called “New Trigger”.  From the drop down list entitled “Begin the task:” check the selected option from the default “On a schedule” to “On an event”.  So you should now see something similar to this screen shot;

At this point I was kind of stumped- The other options in that “Begin the task:” list were things like “At log on”, “At startup”, “On work station lock” etc – there was no “On Shutdown”!! So I selected “On an event”. This gives you three new options in Settings – Log, Source and Event. From here it was down to taking an educated guess. I knew that shutting down Windows was likly to be logged as a “System” event, so in the first drop down I selected System.

What to choose in the Source drop down was not so clear- so to decide on an event source to use, I opened the Event Viewer, again from Control Panel > Administrative Tools. From the left hand panel, I opened the “Windows Logs” branch, then from within that selected “System”.  These are arranged by time, so it’s just a matter of going back to when you turned your computer on today, then going back a couple more to see the last things your computer logged before turning off when you last used it.

In the end I opted for the event shown in the above screen shot- by reading the description I noticed it mentioned shutting down explorer.exe (a main component in Windows) because of the “power off of computer”, which I figured, with out knowing the in’s and out’s of what this really meant, sounded pretty promising. So from the same window I noted the Source “USER32″ and Event ID “1074″.  Going back to the New Trigger window I found that USER32 did indeed appear in the list, so I selected that and entered 1074 as the event ID.  This is pretty much everything for the trigger, so hit ok, then move on to the Actions tab, and click “New…”

From here it’s pretty easy- the action we want is to “Start a program” then just use the browser button to find the program you want to run. Click OK and return to the General tab.

This final step may not be required, but I did it anyway just to be sure- on the general tab, I changed the radio button at the end and selected “Run whether user is logged on or not”. I also changed the “Configure for:” drom down at the bottom to be “Windows 7, Windows Server 2008 R2″. And that’s it- Shut down your computer to give it a go. If you are following on from my previous post and you pointed the action dialogue to run the .exe produced from the last post, you should see your computer shut down and then a few seconds later the NAS box will power down.

Drop me a comment if I’ve missed any critical details!

You can see the finished app here

No Comments

Automatic NAS Box shutdown – How to POST the shutdown command over SSL with HTTP authentication from a dot net Console app (Part 1/2)

I recently purchased a Netgear ReadyNAS Duo RAIDed network storage box for general backup of my ‘stuff and things’, which sit’s on my desk and runs off the same power as my PC.  A couple of times now I’ve booted the box up to run a backup, then forgot to shut it down, then shut down my PC, flick the switch at the wall and then realise.  When I boot everything back up in a day or so, the NAS box has to do a full system check and maybe rebuild the RAID array if theres a problem etc etc – it’s a pain.

So I thought I would set about automating the NAS box shut down with my PC shut down which turned out to be pretty easy.  The NAS box is controller via a web UI- my box has the host name “BACKUP” so I can get to it over the browser on http://BACKUP/.  This uses basic HTTP authentication asking me to login, and also use’s https (which my browser always warns me about because it’s not from a trusts CA or whatever- so I just ignore the warnings and continue).  From the web UI you can go to the system tab and select shut down and the box goes down.  I figured as this is going to be a simple web call, it should be easy enough to write a simple console app which makes the same call which can be fired on Windows shutdown- so I fired up Firebug, opened the “Net” tab, which allows you to inspect all http calls made, and triggered a shut down. It’s probably worth noting at this point that the techniques used here could probably be applied to pretty much any other networked device with a web based UI (most routers, firewalls, switches, printers, media boxes etc) so this should hopefully be quite useful.

Sure enough the HTTPS POST made to a file called get_handler on the root could be seen, with the POST data string;

As we can see from firebug, the data sent is;

PAGE=System&OUTER_TAB=tab_shutdown&INNER_TAB=NONE&shutdown_option1=1&command=poweroff&OPERATION=set

So six key value pairs separated by ampersands.  The code itself is pretty simple using WebClient class (System.Net namespace)- however there are a couple of complications, which as it happens are fairly common things to run into, so a good opportunity to give some example code for future reference;

  • We need to perform our Form Post over SSL

  • The SSL certificate of the server we are posting to will never be valid!

  • We must carry out our form post while supplying basic HTTP authentication credentials

I created a new Console application and added the following code;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace ShutdownNASBox
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient client = new WebClient())
            {
                // add the basic http authentication username & password
                client.Credentials = new NetworkCredential("admin", "abc123");

                // register the face that the connection uses SSL
                // (otherwise it defaults to TLS and you get an exception)
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

                // set it to effectivly ignore the SSL certificate
                ServicePointManager.ServerCertificateValidationCallback =
                                                        delegate { return true; };

                // build a NameValueCollection which holds those 6 values we saw
                // being posted when we used firebug
                NameValueCollection c = new NameValueCollection();
                c.Add("PAGE", "System");
                c.Add("OUTER_TAB", "tab_shutdown");
                c.Add("INNER_TAB", "NONE");
                c.Add("shutdown_option1", "1");
                c.Add("command", "poweroff");
                c.Add("OPERATION", "set");

                try
                {
                    // Post the data!  This will return as a byte array
                    byte[] bytes =
                    client.UploadValues("https://backup/get_handler", c);

                    // convert the byte array into text we can read!
                    string result = Encoding.ASCII.GetString(bytes);

                    Console.Write("Shutdown Message Sent");
                    Console.Write(result);

                }
                catch (Exception e)
                {
                    Console.Write("Shutdown Message Send Failure!");
                    Console.Write(e.ToString());
                }
            }
        }
    }
}

And that’s all there is to it! An obvious and easy upgrade would be to make this parametrized and maybe get it to write to the windows log on failure.

In part two I shall do a quick post on how to schedule tasks to run on shut down in Windows 7.

You can see the finished app, with the above recommended improvements already implemented, here

, , , ,

No Comments

I absolutely love these. They came at the lace wigs are good quality, and very soft. Absolutely love these extensions. I just love this hair. Hair came on time, I waited until after hair extensions uk very full, no shedding and it is been installed for 3 weeks now and yes I jus ordered 3 more bundles. Lol. I definitely recommend this company for your bundles ladies. They are soft, hair bundles on the head for human hair wigs and I would continue to recommend them. I have thinner hair so they cover and blend nicely.