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?
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?
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"...
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
--
WBR, Vadims Podans
PowerShell blog - www.sysadmins.lv
"RickB" <rbie...@i1.net> rakstīja ziņojumā
"news:1669d224-5561-4f07...@j8g2000yql.googlegroups.com"...
That is exactly what I did.
Useless
Those scripts are not run until after all applications shut down or
are forced down.
On Mar 11, 2:44 pm, Bob Landau <BobLan...@discussions.microsoft.com>
wrote:
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.
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
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.
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!
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
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