Run the batch file when shutting down Windows 7 before closing the programs (preferably press the power button)

I have a Windows 7 window with several VMWare player machines that start at boot. I use WMWare VIX and a batch file to shut down virtual machines as follows:

"C:\path\to\VMWare VIX\vmrun.exe" -T player stop "C:\path\to\machine.vmx" soft 

What I want to execute is to run these commands when the host computer is off, so I do not need to shut down each virtual machine separately. When deploying a shutdown, it will most likely start with a short press of the ACPI power button, and not from the Start menu. The methods I've tried that don't work (at least not enough):

  • Group Policy is the most obvious way out, but Windows 7 has disabled the ability to run asynchronous shutdown scripts.
    As a result, Windows first tells all open windows to close, the VM responds that they are being used, and you get a "Force close" dialog. Only after VMWare Player and everything else are closed, scripts are launched, but not used.
    You might think that this could be changed, but I think that I remember seeing an official MS note on the subject “no, sorry.” Unable to find link.

  • Use one batch file that closes all virtual machines and then disconnects the host as a shortcut on the desktop instead of the usual shutdown button. - It works, and that I use now, in the development.
    But using the ACPI power button will initiate a normal shutdown with the same result as before, and it would be better if the end user, who turned the machine on and off daily, would not need to use a monitor and mouse. <br> So what I'm looking for on Google right now is a way to change the action caused by pressing the physical power button. Windows allows you to choose between various actions, such as Sleep, Hibernate, Restart, etc., But can you change this to "Run this .bat"? Or maybe to change the behavior of the shutdown command altogether?

  • Programmatically intercept the shutdown message, interrupt shutdown, run the batch file, restart the shutdown. There was some discussion of stop interception, for example. here , here and here , but I'm still too many n00b in all languages ​​except Ruby or Java to really understand if and how this can be done in this case. If someone can clarify how to actually do this work (without getting stuck on the “Closing Force” screen), then I want to try any language that you offer.

+8
windows-7 vmware batch-file shutdown acpi
source share
3 answers

Ok, so I found a solution that worked for me; a tool called AutoHotkey_L and a script made according to these threads on the AutoHotkey forums.

This is the code I use and suggest reading AutoHotkey commands in the documentation. I tweak the code when I find out what it actually does, but for now it works. :)

 #NoEnv #Persistent SendMode Input SetWorkingDir %A_ScriptDir% SetTimer, RunBeforeShutdown, Off Gui,+LastFound hwnd:=WinExist() DllCall("ShutdownBlockReasonCreate","Uint",hwnd,"Str","") DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0) ;puts us first in line for getting the shutdown call, i guess? OnMessage(0x11, "WM_QUERYENDSESSION") Return WM_QUERYENDSESSION(wParam, lParam) { ENDSESSION_Logoff = 2147483648 If (lParam == ENDSESSION_Logoff) { global EventType = "Logoff" } Else { global EventType = "Shutdown" ;no way to distinguish between shutdown and restart } SetTimer, RunBeforeShutdown, On Return false } runBeforeShutdown: SetTimer, RunBeforeShutdown, Off Sleep, 1000 SendInput, {ENTER} ; gets us past the 'Force shudown' screen Sleep, 1000 #SingleInstance, Force DllCall("ShutdownBlockReasonDestroy","Uint",hwnd) ; **** Your commands go here **** RunWait shutdown.bat ; ******** If (EventType == "Logoff") { Shutdown, 0 } Else { Shutdown, 1 } Reload Return 

So, now it only distinguishes between exits and shutdowns, but this post has a simple graphical interface in HTML, which allows the user to choose whether they want to reboot, hibernate, etc.

In my case, it is normal to interrupt the shutdown and run the batch file regardless of whether VMware works or not, but you can set a condition for it, for example:

 IfWinExist, ahk_class VMPlayerFrame { SetTimer, RunBeforeShutdown, On Return false } Else { Return true } 

I already had problems with this script, for example, when the host slowed down so much (memory leak) that the "Force shudown" screen did not appear in time for the script to close it. And this is likely to benefit from tracking the number of attempts so that it can force-disconnect if the first attempt fails.

Good enough, at least. And I don’t even need virtualization for my project, but I hope this can help someone else. Alternative solutions are still very welcome.

+3
source share

I came up with this solution for the same problem: http://communities.vmware.com/thread/334740

The trick to go through a "forced shutdown" is to first pause / stop all virtual machines, and then close the shutdown again in the same script. It seems to work for me.

+1
source share

The workstation has the preference "Keep virtual machines after closing the workstation." Does VMware Leader Have the Same Option?

As I see it working: Include the preference above. The window closes, leaving the virtual machines. Then, the shutdown process can continue with your C # 1 script, which should shut down or pause virtual machines before shutting down.

I do not have my own virtual machines, so I can not verify this, but I hope this helps.

0
source share

All Articles