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
Cleanup on Exit
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.
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.
- SAPIEN Support Forums
- Posts: 945
- Last visit: Thu Oct 22, 2015 1:10 pm
Cleanup on Exit
This is an automated post. A real person will respond soon.
Thank you for posting, ClairmontAle.
Did you remember to include the following?
*** Make sure you do not post any licensing information ***
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.
*** Make sure you do not post any licensing information ***
Re: Cleanup on Exit
I moved this post to the Windows PowerShell forum, since this is a scripting question and not a product question.
David
David
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Cleanup on Exit
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.
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.
- ClairmontAle
- Posts: 9
- Last visit: Tue Feb 12, 2019 10:30 am
Re: Cleanup on Exit
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.
Re: Cleanup on Exit
I found this on stack overflow:
http://stackoverflow.com/questions/10174075/powershell-window-preventing-shutdown
David
http://stackoverflow.com/questions/10174075/powershell-window-preventing-shutdown
David
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Cleanup on Exit
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.
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.
- ClairmontAle
- Posts: 9
- Last visit: Tue Feb 12, 2019 10:30 am
Re: Cleanup on Exit
Looks like this might work looking at the code. I'll give it a try and report back.davidc wrote:I found this on stack overflow:
stackoverflow[.]com/questions/10174075/powershell-window-preventing-shutdown
David
Re: Cleanup on Exit
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.
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.
Re: Cleanup on Exit
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
http://windowsitpro.com/systems-managem ... just-do-it