Answered by:
VBScript: Detect the logoff reason in a logoff script

Question
-
Hi,
I'm trying to detect the "logoff reason" in a logoff script.
I need to find out whether the system is restarted / shut down or only a logoff is performed. My script must be executed in the context of the currently logged on user, but it should only run when the pc will be restartet or shut down.
I tried to query the newes entries from the event log using WMI, but I couldn't detect useful informations at the time of execution.
Are there any possiblities to detect the "logoff reason" in a logoff script?
It should work without administrative rights and with Windows XP.Many thanks in advance.
Best regards
Thomas- Changed type ThomMa Monday, April 26, 2010 4:53 PM
Wednesday, April 21, 2010 11:31 AM
Answers
-
ThomMA
Just following up if you found an answer to the situation or if you're still looking?
Depending on the situation, if you're trying to force a shutdown via script, the SHUTDOWN command can be supplied with a reason for the shutdown
For example executing a
SHUTDOWN -s -f -t 30 -c "I felt like it was time for a rest"
Will shutdown the operating system forcefully with a logged comment in the System Event logs indicating "I felt like it was time for a rest"
For more details about additional parameters in the shutdown command just execute
SHUTDOWN /?
As for detecting why the logoff occured, I don't know personally if that's available.
Powershell. It's so Easy and it's FREE! Dive in and use it now, It'll take no time. :) http://www.energizedtech.com http://www.itprotoronto.ca- Proposed as answer by Marco Shaw Thursday, October 21, 2010 2:04 AM
- Marked as answer by IamMred Friday, October 22, 2010 6:07 PM
Wednesday, October 20, 2010 10:21 PM
All replies
-
Sounds problematic. Normal shutdown is processed after user logoff, so you aren't in the context of the currently logged on user, because there isn't one. A system can be shut down before logon or after logoff, and can be shutdown/restarted by a hardware reset, or by a system, remote, or scheduled task command.Wednesday, April 21, 2010 11:51 AM
-
Thanks a lot for your assessment.
The reason of this problem is, that I want to execute a script at shut down or restart primarily. But the used APIs requires an active user context.
Hence, the script must be performed at the users logoff with a condition to cancel the script execution.Is there no global "flag" that indicates that a shut down or restart progress is running or initiated?
Wednesday, April 21, 2010 12:38 PM -
There may be a way to detect that there is a shutdown in progress, but then you're faced with the prospect of convincing it to start a new process after it's already initiated a shutdown.Wednesday, April 21, 2010 12:59 PM
-
Can you tell me the way to detect an active shutdown? I think it won't be a problem that the logoff script use some new external processes to do it's job.Wednesday, April 21, 2010 1:53 PM
-
- Marked as answer by IamMred Wednesday, April 21, 2010 9:58 PM
Wednesday, April 21, 2010 1:59 PM -
Thanks again for your standby.
Unfortunately, the two discussed solution for this problem doesn't work in scripts.
GetSystemMetrics
This function cann't be directly calle from VBScript, therefore I build a little wrapper application to call Windows APIs from VBScript. But the function returns alway zero, also during the shutdown progress.WM_QUERYENDSESSION
To process the WM_QUERYENDSESSION event, I must be create an application or service in order to use subclassing.I can't believe that there isn't any information that can be queried using WQL...but it seems not to be as easy as expected,unfortunately.
Friday, April 23, 2010 2:48 PM -
ThomMA
Just following up if you found an answer to the situation or if you're still looking?
Depending on the situation, if you're trying to force a shutdown via script, the SHUTDOWN command can be supplied with a reason for the shutdown
For example executing a
SHUTDOWN -s -f -t 30 -c "I felt like it was time for a rest"
Will shutdown the operating system forcefully with a logged comment in the System Event logs indicating "I felt like it was time for a rest"
For more details about additional parameters in the shutdown command just execute
SHUTDOWN /?
As for detecting why the logoff occured, I don't know personally if that's available.
Powershell. It's so Easy and it's FREE! Dive in and use it now, It'll take no time. :) http://www.energizedtech.com http://www.itprotoronto.ca- Proposed as answer by Marco Shaw Thursday, October 21, 2010 2:04 AM
- Marked as answer by IamMred Friday, October 22, 2010 6:07 PM
Wednesday, October 20, 2010 10:21 PM -
I don't know much about vbscript, but if you can query the registry then you can do it - see http://technet.microsoft.com/en-us/library/cc962586.aspx .
I have done it in a CMD file as described here: http://www.robvanderwoude.com/ntregistry.php
Hope that helps
Dave
Project African Wilderness: Promoting conservation and development in Malawi
Sunday, February 13, 2011 10:21 AM