title: Run a batch file on Windows 7 shutdown before closing programs (preferably on power button press)
body: <p>I have a Windows 7 box with multiple VMWare Player machines that are started on boot. I use WMWare VIX and a batch file to shutdown the virtual machines like so:  </p>
  
  <pre><code>"C:\path\to\VMWare VIX\vmrun.exe" -T player stop "C:\path\to\machine.vmx" soft
  </code></pre>
  
  <p>What I want to accomplish is to run these commands when the host machine is shut down, so that I don't need to shut down each VM separately. When deployed, shutdowns will likely be started with a short press of the ACPI power button, not from the Start menu. Ways I've tried that don't work (at least not well enough):  </p>
  
  <ol>
  <li><p>Group Policies - The most obvious way to go, but in Windows 7, the option to run shutdown scripts 'asynchronously' is gone.<br>
  The result is that Windows <em>first</em> tells all open windows to close, the VMs respond that they are in use and you get the 'Force close' dialog. Only <em>after</em> VMWare Player and everything else is closed are the scripts run, to no use.<br>
  You'd think this could be changed, but I think I remember seeing some official MS note along the lines of "nope, sorry". Can't find the link though.</p></li>
  <li><p>Use one batch file that closes all VMs and then shutdowns the host as a desktop shortcut instead of the usual shutdown button. - Works, and that's about what I'm using right now while developing.<br>
  But using the ACPI power button initiates a normal shutdown with the same result as earlier, and it would be better if the end-user who turns the machine on and off on a daily basis wouldn't need to use a monitor and mouse.<br>
  So what I'm googling for at the moment is a way to modify the action called when pressing the physical power button. Windows allows you to choose between some different actions like Sleep, Hibernate, Restart etc, but could you change that into 'Run this .bat'? Or maybe change the behaviour of the shutdown command altogether?</p></li>
  <li><p>Programmatically intercept the shutdown message, abort shutdown, run batch file, re-initiate shutdown. There has been some discussion on intercepting shutdown e.g. <a href="https://stackoverflow.com/questions/1184940/programmatically-prevent-windows-to-shutdown">here</a>, <a href="https://stackoverflow.com/questions/3490252/intercept-shutdown-in-windows-using-vbscript">here</a> and <a href="https://stackoverflow.com/questions/2487185/block-windows-shutdown">here</a>, but I'm still too much of a n00b in all languages except maybe Ruby or Java to really understand if and how it could be done in this case. If someone can clarify how to actually make this work (without getting stuck on the 'Force close' screen) then I'm eager to try out any language you offer.</p></li>
  </ol>