I've had a number of problems where the shutdown fails and the error is something like "computer not ready" or "not in a suitable state".

A very simple example is where the username/password login dialogue is active, but there have been other causes. I imagine that during startup it is pretty likely that the machine will be in various states when Windows will reject shutdown requests because they are potentially dangerous.

You could put the shutdown in a simple loop, and if it errors wait a period (30 seconds?) and then try again. After a 10 or 15 minutes of retries give it up as a bad job.