• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1821
  • Last Modified:

Exit/Close/Kill Powershell process on OS shutdown

Hello,

I'm working on a powershell script that use System.IO.FileSystemWatcher class.
I run the script in background (1 powershell process is create with the -noexit option)
BUT when the workstation initiate a reboot/shutdown, the powershell process (like a cmd process) don't close (exit) automatically !!! :((( until the user choose to terminate the process !
(i m using windows XP)

is there a solution to kill my powershell process ?? or something else

Tkx in advance.
Anthony
0
kilz3m
Asked:
kilz3m
  • 6
  • 5
1 Solution
 
kilz3mAuthor Commented:
or is there another way to launch powershell script in background ?
I tryed invoke-command but WSMAN:\localhost\shell\idletimeout (or $pssessionoption.idletimeout = 0), on both side, didn t work (le session closed and didn t stay alive).
0
 
kilz3mAuthor Commented:
well in fact, let me explain :
I need to monitor change on filesystem (file in a directory) and trigger an action if change occur (i m using the .NET System.IO.FileSystemWatcher class)
i run my script (launch by a vbs script in order to hide the powershell window and i use the the NoExit option in order to keep my powershell process open) (1 powershell process is created and stay open), my script create 4 Register-ObjectEvent and the idle until System.IO.FileSystemWatcher event occur.

If i dont use the NoExit option my script execute and close and finaly my Register-ObjectEvent disappear.

My script work like a charm BUTT :
When a user want to shutdown or restart is computer, the powershell process dont automatically close !! until the user choose to terminate the process ! (on windows XP)

is there a solution to AUTOMATICALLY kill my powershell process ON SHUTDOWN/RESTART event ?? or something else ?
0
Live Q & A: Securing Your Wi-Fi for Summer Travel

Traveling this summer? Join us on June 18, 2018 for a live stream to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
x-menIT super heroCommented:
gpedit.msc - Computer Compiguration - Windows Settings -Scripts (Shutdown)

Place a "taskkill.exe /im powershell.exe" batch or script in it
0
 
x-menIT super heroCommented:
or maybe:
Name: Taskill.exe
Parameters: /IM powershell.exe

knock yourself out ;)
0
 
kilz3mAuthor Commented:
User and Computer GPO script execute only after every user process have been killed ... ://
My powershell process block the gpo script execution !
0
 
x-menIT super heroCommented:
how about a Logoff script?
0
 
kilz3mAuthor Commented:
a logoff script is a script that configured by GPO (User Setting) or Localy with gpedit.msc (User Configuration).
And is executed when every user process are closed and logoff process continu to execute script in group policy setting.

In my case, powershell process block the logoff process and the script is never executed !
0
 
x-menIT super heroCommented:
how about changing the logof behavior on the registry, forcing the shutdown of running applications

check this:
http://www.johntp.com/2006/02/14/speed-up-windows-xp-shutdown/
0
 
kilz3mAuthor Commented:
thank you x-men
i only found this solution too, but it s a litle violent ;)

Set-ItemProperty 'hkcu:\control panel\desktop' AutoEndTasks 1

I think that there is no other solution for Windows XP
0
 
kilz3mAuthor Commented:
a litle violent ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now