Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 396 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell profile "on exit" event?

#1
I'm looking for a way to automatically do some clean up tasks when the PowerShell session quits. So for example in my profile file I start a process which needs to run in the background for quite a lot of tasks and I would like to automatically close that process when I close the console.

Is there some function the PowerShell automatically calls when closing the session as it does with `prompt` when displaying the prompt?
Reply

#2
There is the [`Register-EngineEvent`][1] cmdlet which you can use to attach an event handler to the `Exiting` event:

Register-EngineEvent PowerShell.Exiting -Action { ... }

Note however, that this event will not be fired if you close the console window.


[1]:

[To see links please register here]

Reply

#3
For an example of how to on exit event. In this case, saving the History of commands on to a file, and maybe stored in a $Profile.

> To automatically export the previous commands to a file at the end of
> a PowerShell session, you can bind the script to the PoSh session
> termination event (!! The session must be necessarily ended with the
> exit command, rather than simply closing the PowerShell
> window):
>
> $HistFile = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history
> Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistFile } | out-null
> if (Test-path $HistFile) { Import-Clixml $HistFile | Add-History }


Quoted from

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through