How to run a task when shutting down

P

paroots

I wish to run a task (to dismount a network hard drive) whenever shutting
down. I have noticed that the Task Scheduler has a trigger called, 'On local
disconnect from any user session'. I have setup a task based on this trigger,
but it doesn't execute when I do a shutdown. Any ideas are much appreciated
 
S

solon fox

I wish to run a task (to dismount a network hard drive) whenever shutting
down. I have noticed that the Task Scheduler has a trigger called, 'On local
disconnect from any user session'. I have setup a task based on this trigger,
but it doesn't execute when I do a shutdown. Any ideas are much appreciated

Great question. I'd just like to point out that at disconnect from
user session is not the same as shutdown. I suspect that shutdown
begins a series of events that simply doesn't wait for your task to
even start let alone finish.

I would be very interested to learn how to force something to run at
shutdown.

Unless someone figures it out, why not just run your script at
startup? It seems to me that the end result would be very close.

-solon fox
 
P

paroots

Thanks for the responses; they're much appreciated. I googled on gpedit.msc
and discovered that it is a Windows XP feature? Is it available in Vista and
if so, how?
 
M

Mark L. Ferguson

Click Start, and type:
gpedit.msc
hit enter
The path to the logoff script setting is:
Local Computer Policy/Computer Configuration/Windows
Settings/Scripts(Startup/Shutdown/...
It can, of course, be done on a per user basis at:
LCP/User Configuration/...
 
P

paroots

Hello Mark:
I've googled on gpedit.msc and have not found any indication that this
command is available in Windows Vista Home Premium. Perhaps I have something
else that accomplishes the same thing. Any ideas?
 
P

paroots

Thanks Rick. I thought I was losing my mind. Anyone have any ideas how to
scheule a batch file to run on shutdown?
 
M

Mark L. Ferguson

It's possible to launch a hidden IE window at startup (wscript shell.run
command, second argument). This could run a *.HTA file and would allow
scripting, to have a set of script commands run 'OnExit' for the .hta page.
(This would only work with the consent, or lack of ability of the user.)
 
S

solon fox

Hey paroots,

Have you thought about adding shutdown as the last command of the
script you want to run? Instead of shutting down the normal way, you
could maybe just run your script?

The command to shutdown is:
RunDll32.exe shell32.dll,SHExitWindowsEx 1

The commad to restart is:
RunDll32.exe shell32.dll,SHExitWindowsEx 2

Here's a simple VBS script as an example.

'BEGIN SCRIPT ***************************************
'Restart the desktop

Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'END OF SCRIPT ***************************************

-solon fox
 
P

paroots

thanks solon fox. I like your idea, but can't make it work. When I create
'test2.vbs' and run your script I get the following error message:
Error in shell32.dll
Missing entry:SHExitWindowsEx
Also, I don't understand why your script is doing a restart?
 
S

solon fox

Well, I'm confused. I can't imagine you not having the SHExitWindowsEX
method in shell32.exe. All, I can guess at is either you are running
Vista 64 (I assumed 32), or some kind of error in copying what I
wrote.


The method, SHExitWindowsEx, accepts several parameters. Here are more
examples:
~*~*~*~*~
'This will log the user off.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 0"

'This will shutdown the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'This will restart the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 2"

'This will do a forced shutdown.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 4"

'This will power down the machine.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 8"

~*~*~*~*~*~*~*~*~
I myself, left a comment error ('Restart the desktop), in the sample
script that I gave you.

'1' shuts down. '2' restarts, '4' does an unfriendly shutdown. '8'
shuts off the power also unkind.

So... let's take the script from the top with correct comments and see
if this helps you. I shall show you two scripts one that uses shell32
just like the original and another that uses the WMI (maybe that will
work better for you?)

'BEGIN Copy below this line
--------------------------------------------------------
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
later
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
'oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"


'END Copy above this line
---------------------------------------------------

Note the syntax: RunDll32.exe [space]
shell32.exe[comma]SHExitWindowsEX [space] 1

Now, let's take a look at using WMI - which is great if you can't get
the above to work. It is "safer" more elegant, but less direct and
more lines

'BEGIN Copy below this line -------Shutdown with
WMI-------------------------------------------------
'define the SHUTDOWN constant as 1
Const SHUTDOWN = 1

'set the string to this computer
strComputer = "."

'define th object that we need
Set objWMIService = GetObject_
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

'define our OS as Win32
Set colOperating Systems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

'tight loop shuts down applications, services and the session
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

'END Copy above this line
---------------------------------------------------

I hope this works for you.

If still no luck, then feel free to write my gmail account and I can
send attachments. I can't swear to no typo's in the above, but I did
run both scripts.

-solon fox
 
S

solon fox

I see another error. I left a line commented! The first script would
do nothing!

Here it is again:
'BEGIN Copy below this line
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'END Copy above this line

The second script also has an error caused by word wrap. The line
"WMI------------------------------------------------- " should not be
copied.

-solon fox



Well, I'm confused. I can't imagine you not having the SHExitWindowsEX
method in shell32.exe. All, I can guess at is either you are running
Vista 64  (I assumed 32), or some kind of error in copying what I
wrote.

The method, SHExitWindowsEx, accepts several parameters. Here are more
examples:
~*~*~*~*~
'This will log the user off.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 0"

'This will shutdown the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'This will restart the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 2"

'This will do a forced shutdown.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 4"

'This will power down the machine.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 8"

~*~*~*~*~*~*~*~*~
I myself, left a comment error ('Restart the desktop), in the sample
script that I gave you.

'1' shuts down. '2' restarts, '4' does an unfriendly shutdown. '8'
shuts off the power also unkind.

So... let's take the script from the top with correct comments and see
if this helps you. I shall show you two scripts one that uses shell32
just like the original and another that uses the WMI (maybe that will
work better for you?)

'BEGIN Copy below this line
--------------------------------------------------------
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
later
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
'oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'END Copy above this line
---------------------------------------------------

Note the syntax: RunDll32.exe [space]
shell32.exe[comma]SHExitWindowsEX [space] 1

Now, let's take a look at using WMI - which is great if you can't get
the above to work. It is "safer" more elegant, but less direct and
more lines

'BEGIN Copy below this line -------Shutdown with
WMI-------------------------------------------------
'define the SHUTDOWN constant as 1
Const SHUTDOWN = 1

'set the string to this computer
strComputer = "."

'define th object that we need
Set objWMIService = GetObject_
    ("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
        strComputer & "\root\cimv2")

'define our OS as Win32
Set colOperating Systems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

'tight loop shuts down applications, services and the session
For Each objOperatingSystem in colOperatingSystems
    objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

'END Copy above this line
---------------------------------------------------

I hope this works for you.

If still no luck, then feel free to write my gmail account and I can
send attachments. I can't swear to no typo's in the above, but I did
run both scripts.

-solon fox

thanks solon fox. I like your idea, but can't make it work. When I create
'test2.vbs' and run your script I get the following error message:
   Error in shell32.dll
   Missing entry:SHExitWindowsEx
Also, I don't understand why your script is doing a restart?
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
P

paroots

Thanks again solon fox. Your latest script still generates an error on my
Windows Vista Home Premium 32 bit computer. Here is the script I'm testing
(between the hyphens):
--------------
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"
-------------

and here is the error message received:
------------
Error in shell32.dll
Missing entry:SHExitWondowsEx
------------

by the way, I have learned from the manufacturer of the NDAS drive that I
will want to execute the program ndascmd.exe with options. I know how I would
do this in a DOS batch file; how would I invoke this in this vbs script file?
Thanks.

solon fox said:
I see another error. I left a line commented! The first script would
do nothing!

Here it is again:
'BEGIN Copy below this line
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'END Copy above this line

The second script also has an error caused by word wrap. The line
"WMI------------------------------------------------- " should not be
copied.

-solon fox



Well, I'm confused. I can't imagine you not having the SHExitWindowsEX
method in shell32.exe. All, I can guess at is either you are running
Vista 64 (I assumed 32), or some kind of error in copying what I
wrote.

The method, SHExitWindowsEx, accepts several parameters. Here are more
examples:
~*~*~*~*~
'This will log the user off.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 0"

'This will shutdown the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'This will restart the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 2"

'This will do a forced shutdown.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 4"

'This will power down the machine.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 8"

~*~*~*~*~*~*~*~*~
I myself, left a comment error ('Restart the desktop), in the sample
script that I gave you.

'1' shuts down. '2' restarts, '4' does an unfriendly shutdown. '8'
shuts off the power also unkind.

So... let's take the script from the top with correct comments and see
if this helps you. I shall show you two scripts one that uses shell32
just like the original and another that uses the WMI (maybe that will
work better for you?)

'BEGIN Copy below this line
--------------------------------------------------------
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
later
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
'oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"

'END Copy above this line
---------------------------------------------------

Note the syntax: RunDll32.exe [space]
shell32.exe[comma]SHExitWindowsEX [space] 1

Now, let's take a look at using WMI - which is great if you can't get
the above to work. It is "safer" more elegant, but less direct and
more lines

'BEGIN Copy below this line -------Shutdown with
WMI-------------------------------------------------
'define the SHUTDOWN constant as 1
Const SHUTDOWN = 1

'set the string to this computer
strComputer = "."

'define th object that we need
Set objWMIService = GetObject_
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

'define our OS as Win32
Set colOperating Systems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

'tight loop shuts down applications, services and the session
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

'END Copy above this line
---------------------------------------------------

I hope this works for you.

If still no luck, then feel free to write my gmail account and I can
send attachments. I can't swear to no typo's in the above, but I did
run both scripts.

-solon fox

thanks solon fox. I like your idea, but can't make it work. When I create
'test2.vbs' and run your script I get the following error message:
Error in shell32.dll
Missing entry:SHExitWindowsEx
Also, I don't understand why your script is doing a restart?
:
Hey paroots,
Have you thought about adding shutdown as the last command of the
script you want to run? Instead of shutting down the normal way, you
could maybe just run your script?
The command to shutdown is:
RunDll32.exe shell32.dll,SHExitWindowsEx 1
The commad to restart is:
RunDll32.exe shell32.dll,SHExitWindowsEx 2
Here's a simple VBS script as an example.
'BEGIN SCRIPT ***************************************
'Restart the desktop
Set oWshShell = WScript.CreateObject("WScript.Shell")
'This will shutdown the computer.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"
'END OF SCRIPT ***************************************- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
S

solon fox

Thanks again solon fox. Your latest script still generates an error on my
Windows Vista Home Premium 32 bit computer. Here is the script I'm testing
(between the hyphens):
--------------
'Script to shutdown using shell32

'Defines and creates the shell object for later use so we can call it
Set oWshShell = WScript.CreateObject("WScript.Shell")

'This will shutdown the computer. calls the method shell32.dll.
oWshShell.Run "RunDll32.exe shell32.dll,SHExitWindowsEx 1"


Well, it is clearly not you. They have changed shell32.dll - even
though the method is still in my Vista Ultimate version. I would try
the more modern version using the WMI that I posted previously.

'BEGIN Copy below this line -
Const SHUTDOWN = 1

strComputer = "."

Set objWMIService = GetObject_
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

Set colOperating Systems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

'END Copy above this line
by the way, I have learned from the manufacturer of the NDAS drive that I
will want to execute the program ndascmd.exe with options. I know how I would
do this in a DOS batch file; how would I invoke this in this vbs script file?

Here is an example that runs notepad. I assume that it would be
similar for ndascmd.exe - though I haven't used it.

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "notepad.exe c:\scripts\test.txt"
<<snip>>

Here is a great resource for scripting.
http://www.microsoft.com/technet/scriptcenter/resources/qanda/default.mspx

You may want to download the scripting archive with loads of examples
and explanations.
http://www.microsoft.com/downloads/...da-923a-4744-8289-afb73f6a5ed8&displaylang=en

I'll help where I can.

Good luck,
-solon fox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top