Cleanup on Exit

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 4 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ClairmontAle
Posts: 9
Last visit: Tue Feb 12, 2019 10:30 am

Cleanup on Exit

Post by ClairmontAle »

I'm working on a script that works fairly well, but I've encountered an issue where users are turning off their PC while files are being downloaded from Sharepoint to a cache folder. The cache folder contents remain there.

I'm looking for a way to clean up the cache directory on exit. Kind of like the "FormClosing" event, but without a form. Is there a way to define a function or scriptblock to run on exit? Maybe there's a way to listen for the process kill event Windows sends on Shutdown?

Powershell Studio 2012 v3.1.16 32-bit
Windows 7 SP1 64-bit
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Cleanup on Exit

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, ClairmontAle.

Did you remember to include the following?
  • 1. Product, version and build (e.g. Product: PowerShell Studio 2014, Version & Build: 4.1.71. Version and build information can be found in the product's About box accessed by clicking the blue icon with the 'i' in the upper right hand corner of the ribbon.)
    2. Specify if you are running a 32 or 64 bit version
    3. Specify your operating system and if it is 32 or 64 bit.
    4. Attach a screenshot if your issue can be seen on the screen
    5. Attach a zip file if you have multiple files (crash reports, log entries, etc.) related to your issue.
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Cleanup on Exit

Post by davidc »

I moved this post to the Windows PowerShell forum, since this is a scripting question and not a product question.


David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cleanup on Exit

Post by jvierra »

This is not really a scripting issue.

You can run a logoff script. Define the script in Group Policy and it will run when the user logs off.

If user turn off their machines then you are obviously out of luck. We cannot run a script on a machine that has no power.
User avatar
ClairmontAle
Posts: 9
Last visit: Tue Feb 12, 2019 10:30 am

Re: Cleanup on Exit

Post by ClairmontAle »

I understand that, but the script is running when they turn off their PC, so the script breaks out and exits without cleaning up, I'm just looking for a way to execute a scriptblock on exit.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Cleanup on Exit

Post by davidc »

I found this on stack overflow:

http://stackoverflow.com/questions/10174075/powershell-window-preventing-shutdown

David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cleanup on Exit

Post by jvierra »

If the powerbutton is used you cannot over ride it. YOumust have a machine that allows the BIOS to disable the power button.

A logoff or a shutdown command will always call the logoff script. If other things are running the user will be prompted to exit or save. The user can abort all scripts but cannot abort a logoff script.
User avatar
ClairmontAle
Posts: 9
Last visit: Tue Feb 12, 2019 10:30 am

Re: Cleanup on Exit

Post by ClairmontAle »

davidc wrote:I found this on stack overflow:

stackoverflow[.]com/questions/10174075/powershell-window-preventing-shutdown

David
Looks like this might work looking at the code. I'll give it a try and report back.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cleanup on Exit

Post by jvierra »

That won't help if the power button is used.
A logoff script accomplishes exactly the same thing and might be easier.

A logoff scrip prevents logoff from completing until the script has exited.

The system event will work too if you can understand how to implement it. The example seems a bit backwards.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cleanup on Exit

Post by jvierra »

Here is how to configure Windows to automatically cleanup al cached and temporary files automatically and in the background.

http://windowsitpro.com/systems-managem ... just-do-it
This topic is 9 years and 4 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked