deetle
deetle

Reputation: 397

How to do some cleanup on a ctrl+break before the scripts ends

I use Start-Transcript and Stop-Transcript in my script. The issue is if someone does a ctrl+break in ISE with the read button , or just a ctrl+c in the window. The script ends , but Stop-Transcript never gets called. This locks the file and I need to do a manual Stop-Transcript to unlock the file.

So how can this be solved , that even on a ctrl+c , I can do this cleanup ?

In win32 I know I can register a function that gets called and I would be able to do this.

Upvotes: 1

Views: 744

Answers (1)

Philip Meholm
Philip Meholm

Reputation: 391

The easiest thing you can do is implement try, catch, finally:

Start-transcript
try{
   #your code here
}
catch{
    #all error end up here, if they are breaking errors
    Throw $_
}
finally{
    #this will run almost no matter what happens. even on break, errors and ctrl+c
    Stop-transcript
}

Upvotes: 4

Related Questions