Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting system shutdown.

1,185 views
Skip to first unread message

RickB

unread,
Mar 11, 2009, 1:54:57 PM3/11/09
to
I need to run a PowerShell script on our Server 2003 machine any time
the system gets shut down.
Marco pointed me to this link back in December but it didn't end up
being useful.

http://www.microsoft.com/technet/scriptcenter/topics/gp/extension1.mspx

It mentions HKLM\SOFTWARE\Policies\Microsoft\Windows\System\Scripts
\Shutdown as the point of interest on Server 2003 and XP machines but
I can't figure out how to add the required entries and this link
doesn't address that aspect of the location's usefulness.

I've seen references to using gpedit.msc to add a script to startup
and shutdown and tried it on my XP maching but it doesn't seem to even
try to execute. There are no messages in any log indicating it
actually tried to run and I didn't see any entries in the registry
after adding the script using gpedit.msc although the entries were
still visible in gpedit after the reboot.

Has anyone actually added such a script that works?

RickB

unread,
Mar 11, 2009, 2:36:03 PM3/11/09
to
Is there any way for powershell to detect that the OS is shutting
down?
The routine is in an endless loop calling $MEW.WaitForNextEvent
Where:
$MEW = new-object system.management.ManagementEventWatcher(
"SELECT * FROM __InstanceCreationEvent
WHERE TargetInstance ISA 'Win32_NTLogEvent'
AND TargetInstance.LogFile = '$LogName'")

I'm back looking for a way to gracefully shut down this PowerShell
script when the system is rebooted.

I finally got a shutdown script loaded into the


HKLM\SOFTWARE\Policies\Microsoft\Windows\System\Scripts

key to execute and discovered it was a total waste of time.

My script was going to write a message to the log being monitored
telling that PowerShell to shutdown. Unfortunately the OS forcably
stops all applications including my PowerShell before it ever attempts
to run these 'shutdown' scripts (duuh).

Thus I'm back to looking for another way....

Any ideas?

Vadims Podans

unread,
Mar 11, 2009, 3:45:15 PM3/11/09
to
easiest way:
Start -> Run.. -> gpedit.msc
Computer Configuration -> Windows Settings -> Scripts (Startup/Shutdown)

here is no need to use scripts. If you have domain - put this setting to
Default Domain Policy and be happy :)
--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv

"RickB" <rbie...@i1.net> rakstīja ziņojumā
"news:1669d224-5561-4f07...@j8g2000yql.googlegroups.com"...

Bob Landau

unread,
Mar 11, 2009, 3:44:22 PM3/11/09
to
For native applications you can either use register a handler via
SetConsoleCtrlHandler or key on the window message WM_QUERYENDSESSION. Either
of these will work depending on what shell the script is running in but I
don't know of a direct way of doing this .NET

There is also a Wmi class Win32_ComputerShutdownEvent never used it though


If all you're interested in is cleanly closing thiis script look at the
try/catch/finally for v2

Vadims Podans

unread,
Mar 11, 2009, 3:46:40 PM3/11/09
to
and remeber, that Startup/Shutdown scripts will be run under LocalSystem
account.

--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv

RickB

unread,
Mar 11, 2009, 5:27:09 PM3/11/09
to
On Mar 11, 2:45 pm, "Vadims Podans" <vpodans> wrote:
> easiest way:
> Start -> Run.. -> gpedit.msc
> Computer Configuration -> Windows Settings -> Scripts (Startup/Shutdown)
>
> here is no need to use scripts. If you have domain - put this setting to
> Default Domain Policy and be happy :)
> --
> WBR, Vadims Podans
> PowerShell blog -www.sysadmins.lv
>
> "RickB" <rbiel...@i1.net> rakstīja ziņojumā
> >> Has anyone actually added such a script that works?- Hide quoted text -
>
> - Show quoted text -

That is exactly what I did.
Useless
Those scripts are not run until after all applications shut down or
are forced down.

RickB

unread,
Mar 11, 2009, 5:34:14 PM3/11/09
to
They seem to be static and not available to console apps or services.
Moreover, I don't have the source to PowerShell to add these calls.
Perhaps you can describe a system.management.ManagementEventWatcher
query
or other mechanism by which my PowerShell script can detect them.
That
is exactly what I'm looking for.

On Mar 11, 2:44 pm, Bob Landau <BobLan...@discussions.microsoft.com>
wrote:

RickB

unread,
Mar 11, 2009, 5:35:33 PM3/11/09
to
On Mar 11, 2:46 pm, "Vadims Podans" <vpodans> wrote:
> and remeber, that Startup/Shutdown scripts will be run under LocalSystem
> account.
>
> --
> WBR, Vadims Podans
> PowerShell blog -www.sysadmins.lv
>
> "RickB" <rbiel...@i1.net> rakstīja ziņojumā
> >> Has anyone actually added such a script that works?- Hide quoted text -
>
> - Show quoted text -

The local system account can write a log message and that's all I
need.
Unfortunately these scripts don't run until after my PowerShell has
been
forcably stopped.

RickB

unread,
Mar 11, 2009, 5:42:37 PM3/11/09
to
> > > Is there any way for powershell to detect that the OS is shutting
> > > down?
> > > The routine is in an endless loop calling $MEW.WaitForNextEvent
> > > Where:
> > >   $MEW = new-object system.management.ManagementEventWatcher(
> > >          "SELECT * FROM __InstanceCreationEvent
> > >            WHERE TargetInstance ISA 'Win32_NTLogEvent'
> > >              AND TargetInstance.LogFile = '$LogName'")
>
> > > I'm back looking for a way to gracefully shut down this PowerShell
> > > script when the system is rebooted.
>
> > > I finally got a shutdown script loaded into the
> > > HKLM\SOFTWARE\Policies\Microsoft\Windows\System\Scripts
> > > key to execute and discovered it was a total waste of time.
>
> > > My script was going to write a message to the log being monitored
> > > telling that PowerShell to shutdown.  Unfortunately the OS forcably
> > > stops all applications including my PowerShell before it ever attempts
> > > to run these 'shutdown' scripts (duuh).
> > > Thus I'm back to looking for another way....
> > > Any ideas?

I just ran across this
http://blogs.msdn.com/powershell/archive/2006/07/01/653194.aspx
where a comment thread makes me wonder if the functionality is in V2.

> It sure would be nice to be able to hook a "PSExiting" event in a
> profile script and then save history right before exit.

This requires the EVENTING architecture that we'll be working on for
V2

Jeffrey Snover
Windows PowerShell Architect

Marco Shaw [MVP]

unread,
Mar 11, 2009, 6:56:19 PM3/11/09
to

> I just ran across this
> http://blogs.msdn.com/powershell/archive/2006/07/01/653194.aspx
> where a comment thread makes me wonder if the functionality is in V2.
>
>> It sure would be nice to be able to hook a "PSExiting" event in a
>> profile script and then save history right before exit.
>
> This requires the EVENTING architecture that we'll be working on for
> V2
>
> Jeffrey Snover
> Windows PowerShell Architect

The v2 CTP3 SDK has this listed:
PSEngineEvent.Exiting

I don't know if it will apply here, how it works, and if it is "wired
up" (aka "working") in CTP3, but I'll try to find out.

Marco

--
*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

rferrisx

unread,
Mar 11, 2009, 7:43:16 PM3/11/09
to
Generally, I would probably use Network Management or Systems
Management Software to accomplish this and many other tasks requiring
Network Management. HP has some free if you run their Windows
Servers, although it requires you have a Management Server. Microsoft
has extensive Systems Management| Operation Management software/
agents. I am sure both these software create enterprise alerts of
many types (e-mail, logging, etc) with elaborate ping sweeps, WMI
queries, DMA queries, SMB queries, etc.

Using the Performance Monitor Alerting, you could possibly add a
Powershell script as a consequent action to the System | SystemUpTime
alert in perfmon...although I am not sure exactly what that alert
would trace: "If SystemUpTime less than <UNIT TIME> send alert or run
ps script" would work if the system rebooted. Another thought might
be to create alerts on your management computer for TCP | Connections
active. Most probably perfmon alerting has a .NET interface, although
I do not know what that would be off the top Other than that, you
could continually query Event ID 6006 from the System Log ("The Event
Log Service was Stopped") or try to alert on that EventID. Or you
always continually ping or nbtstat your servers from your management
host and send entries to the EventViewer on your 'management host'.
Your best bet is a Network/Systems Management Server, which brings up
the interesting question of how Powershell interacts with Network
Management and Systems Management software.

RickB

unread,
Mar 12, 2009, 2:30:32 PM3/12/09
to
On Mar 11, 5:56 pm, "Marco Shaw [MVP]" <marco.shaw@NO_SPAMgmail.com>
wrote:

> > I just ran across this
> >http://blogs.msdn.com/powershell/archive/2006/07/01/653194.aspx
> > where a comment thread makes me wonder if the functionality is in V2.
>
> >> It sure would be nice to be able to hook a "PSExiting" event in a
> >> profile script and then save history right before exit.
>
> > This requires the EVENTING architecture that we'll be working on for
> > V2
>
> > Jeffrey Snover
> > Windows PowerShell Architect
>
> The v2 CTP3 SDK has this listed:
> PSEngineEvent.Exiting
>
> I don't know if it will apply here, how it works, and if it is "wired
> up" (aka "working") in CTP3, but I'll try to find out.
>
> Marco
>
> --
> *Microsoft MVP - Windows PowerShellhttps://mvp.support.microsoft.com/profile/Marco.Shaw

> *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
> *PowerShell Co-Community Director -http://www.powershellcommunity.org
> *Blog -http://marcoshaw.blogspot.com

That would really be great.
When shutdown occurs, if existing processes are stopped by doing the
equivalent of clicking the [X] and this can detect that I'm all set!

Marco Shaw [MVP]

unread,
Mar 13, 2009, 7:57:26 AM3/13/09
to

>> The v2 CTP3 SDK has this listed:
>> PSEngineEvent.Exiting
>>
>> I don't know if it will apply here, how it works, and if it is "wired
>> up" (aka "working") in CTP3, but I'll try to find out.

No answer yet after asking around...

Marco

--
*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition

Joel Bennett

unread,
Mar 14, 2009, 12:50:02 AM3/14/09
to
In PowerShell 2 you could use the PSEventing stuff...

http://technet.microsoft.com/en-us/library/dd315290.aspx

Basically, you can do this:

Register-EngineEvent -sourceIdentifier `
([System.Management.Automation.PsEngineEvent]::Exiting) `
-action { <# it's your scriptblock, do whatever you like #> }

That WILL fire when PowerShell is being closed, but I don't think you
can use it to do what you're trying to do. There's no way to be sure it
would run BEFORE your other script exited. In fact, it seems to not
fire if you try to register it in the same host. I used this as a test:

Register-EngineEvent -sourceIdentifier `
([System.Management.Automation.PsEngineEvent]::Exiting) `
-action { set-content ~\exiting.txt "Exiting at $(Get-Date)" }

Which works fine if PowerShell is just sitting there, and you close it.
But if I have something like this running in that same host:

sc C:\Users\Joel\exiting.txt "Starting"
$color = 0

While($true) {
if( ${C:\Users\Joel\exiting.txt} -like "Exiting*" ) {
Set-Content ~\SafeExit.txt "Hurrah!"
}
sleep -milli 500
$color = ($color + 1) % 15
Write-Host "." -NoNewLine -Fore $color
}

If I close it while that script is running, the engine event action
script never actually seems to fire (which seems kind-of like a bug?).

--
Joel

0 new messages