Out-Gridview: run scripts when either Cancel or OK buttons pressed?

Welcome Forums General PowerShell Q&A Out-Gridview: run scripts when either Cancel or OK buttons pressed?

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 year ago.

  • Author
    Posts
  • #84443

    Participant
    Points: 1
    Rank: Member

    Powershell V5
    I need to run a different script depending on which of the OK or Cancel buttons are clicked. Is there a simple solution please?

  • #84448

    Participant
    Points: 34
    Rank: Member

    Can you share what you have so far ? Do you mean run script 1 if ok is clicked and run script 2 if cancel is clicked ? or are their multiple ok and cancel buttons on a form ?

    See here for example of an on_click event

    https://blogs.technet.microsoft.com/heyscriptingguy/2014/08/02/weekend-scripter-fixing-powershell-gui-examples/

  • #84454

    Participant
    Points: 1
    Rank: Member

    I am using a standard Out-Gridview (not WPF) and need to run 1 script when OK is clicked and another for the Cancel button. I can find no information anywhere about this.

  • #84457

    Participant
    Points: 0
    Rank: Member

    I guess you could do something like this:

    $Selected = Get-Process | Out-GridView -PassThru
    if($null -eq $Selected) {
        # Cancel was pressed
        # Run cancel script
    }
    else {
        # OK was pressed, $Selected contains what was chosen
        # Run OK script
    }
    
  • #84464

    Participant
    Points: 1
    Rank: Member

    It works nicely and exits with the Cancel button or the X at the top. Thank you, Sir.

The topic ‘Out-Gridview: run scripts when either Cancel or OK buttons pressed?’ is closed to new replies.