Which Logoff/Shutdown API should I call?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I need to write a script that will run every time someone tries to log
off or shutdown a computer. In the past people have logged off
computers while virtual machines were running QE tests or automation.
So it would always be a real pain when these VMs were shut off without
warning. I need to write a script that would check for VMware's
executable file (vmware-vmx.exe) and (if the process is found) prompt
a warning to the user and stop the shut down or log off process from
happening.

I've done A LOT of research on the internet about logoff/shutdown
scripts, posted on message boards and I've emailed people that I
know. So far I've found out that it should be written in vbscript, it
should be added to all computers via group policy, I should write the
script in this way [1. get a process list, 2. search the list for the
VMware process, 3. if process is found alert the user and cancel
shutdown, 4. if not, do nothing], and lastly I have to make API calls
to prevent shutdown/logoff from happening which gets to the point of
my post. I've found four different API calls/messages and I really
don't know which one I should use. I can't move forward with my
scripts until I find out which one I should use:

AbortSystemShutdown - This call aborts a shutdown initiated by
InitiateSystemSutdown or InitiateSystemShutdownEx calls. ASS is
thrown during the timeout period specified by ISS or ISSE.
My qualms: This doesn't work for logging off and I don't know if it
will work for the console session since ISS and ISSE are only used in
RDP connections. Does anyone know if this call work to abort a
shutdown initiated by ExitWindowsEx?

ExitWindowsEx - Can shutdown, logoff, or restart a computer and pass
reasons to the event log as to why any of these happened
My qualms: I haven't found an API that aborts this call. Can you
cancel or abort this call? I know that this API sends the
WM_QUERYENDSESSION message which can potentially abort a logoff/
shutdown if the a value of "0" is returned. How do I respond to this
message with "0" in a script?

WM_QUERYENDSESSION - Whenever a user attemps to logoff or shutdown,
the OS sends this message (via the ExitWindowsEx API) to all open
windows. If the message handler returns "0", windows will abort the
logoff/shutdown.
My qualms: How would I respond to this message in a script? I haven't
found any examples

SystemEvents.SessionEnding - Can cancel shutdown
My qualms: can't cancel logoff and too many caveats: console apps
don't raise this event, cancel only works sometimes, and there's no
guarantee this event will fire before the closing event fires.

Based on my own judgment, I think I should go with WM_QUERYENDSESSION
because it works to cancel both logoff or shutdown which is what I
need. However, I'm a novice at vbscript (started learning three weeks
ago) and this is the first time I've dealt with APIs. *I would
appreciate any help/feedback.*

P.S. Just in case your wondering, I'm aware of the fact that I can't
directly call an API from a script and that I have to use either MS
Office as proxy to load the code into a macro to make the calls or use
an ActiveX control to act as a wrapper to make the calls. Sorry this
post is so long.

.



Relevant Pages

  • RE: Finetuning: Remote Shutdown with WMI, some errors occur.
    ... Also we would like that a workstation that's locked is also shutdown by this ... script to shutdown all approx. ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... Const OpenAsDefault = -2 ...
    (microsoft.public.windows.server.scripting)
  • Re: VBscript that restart the domain comptuer
    ... If you have a shutdown script that cleans up the profiles, ... permissions can restart all computers in a list remotely. ... I have already VB script I ...
    (microsoft.public.windows.server.active_directory)
  • RE: Script or Other Method to Reboot
    ... > We need different privileges when using Shutdown command and shutdown the ... > that only administrator will use command to shutdown the computers. ... > we may need to compile the script. ...
    (microsoft.public.windowsxp.configuration_manage)
  • Re: Finetuning: Remote Shutdown with WMI, some errors occur.
    ... Allows shutdown, poweroff, or reboot of multiple remote systems. ... - generic unhandled script compile or runtime error; ... "Specify 1 and only one shutdown action" ... Dim erNumber, erDesc ...
    (microsoft.public.windows.server.scripting)
  • Re: SBS 2003 R2 Shutdown Problem
    ... The other thing I would do if I had a Trend environment is to do a similar script for Trend as I have done for Exchange. ... scripts that first shutdown Exchange, ... If you create a group policy object - computer shutdown script then you need not worry about forgeting - it'll just run for you! ...
    (microsoft.public.windows.server.sbs)