locked
How can I execute code when I stop script manually ? RRS feed

  • Question

  • For example:
    When I use System.Windows.Forms.Form, there is the event "OnClosing". So, when I close a form, some code can be executed.

    Can I do something like this, when I stop my powershell script when it is running? 

    Tuesday, March 10, 2020 9:28 AM

Answers

  • Seems like a syntax error, try below it will work:

    function Test-Block {
        [cmdletbinding()]
        param (
            # Parameter help description
            [Parameter(Mandatory=$false)]
            [System.String]
            $Str
        )
        try {
            $q = read-Host -Prompt "oo:"
        }
        catch {
    
        }
        finally {
            Write-Host "`nTerminated" -ForegroundColor Red
        }
    }
    
    Test-Block

    • Marked as answer by Messing Jakob Tuesday, March 10, 2020 10:20 AM
    Tuesday, March 10, 2020 10:17 AM

All replies

  • function Test-Block {
        [cmdletbinding()]
        param (
            # Parameter help description
            [Parameter(Mandatory=$false)]
            [System.String]
            $Str
        )
        try {
            $q = read-Host -Prompt "oo:"
        }
        catch {
    
        }
        finnaly {
            Write-Host "terminated"
        }
    }

    When I start this script, I need to type something. 
    I want to press Ctrl+C or stop button in ISE or VS Code and execute code 

    Write-Host "terminated"

    I tried using finally, but it didn`t help

    Tuesday, March 10, 2020 10:13 AM
  • Seems like a syntax error, try below it will work:

    function Test-Block {
        [cmdletbinding()]
        param (
            # Parameter help description
            [Parameter(Mandatory=$false)]
            [System.String]
            $Str
        )
        try {
            $q = read-Host -Prompt "oo:"
        }
        catch {
    
        }
        finally {
            Write-Host "`nTerminated" -ForegroundColor Red
        }
    }
    
    Test-Block

    • Marked as answer by Messing Jakob Tuesday, March 10, 2020 10:20 AM
    Tuesday, March 10, 2020 10:17 AM
  • It doen`t work in VS code but it works in ISE. 

    Please, explain how does Ctrl+C work... 
    Does it raise an exception? 

    Tuesday, March 10, 2020 10:24 AM
  • CTRL+C generally works as a terminate action for the powershell ISE. You could think of it as an interrupt signal that would terminate the running process (script). You could refer below for detailed explanation:

    https://blogs.technet.microsoft.com/dsheehan/2018/10/27/powershell-taking-control-over-ctrl-c/

    I guess that the windows processes could have been able to customize the behavior of such key presses, what is a terminating signal for powershell might not be the same for VSCode. That might be the reason why it would not be working on the VS Code. It's just a blind guess.

    Please let me know if you find the exact cause of such behavior, it would be helpful.

    Tuesday, March 10, 2020 10:35 AM