Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

PurePowerShell post – GUI Pause to give user choice to continue or abort a PowerShell script execution

$
0
0

<# Put the below between the #BEGIN PAUSE DEFINITION# and #END PAUSE DEFINITION# tags at the beginning of your script, and whenever you need a pause, just type the "GUIPAUSE" word to call the function when you need to give user the choice to continue or abort a script execution

 

If you ever need to customize the pause window, use the following format:

 

  GUIPAUSE -Message "MyMessage" -Title "MyTitle"

 

 

 

 -------------------------- EXAMPLE 1 --------------------------

 

 GUIPAUSE

 

 => launches the Pause window with "OK" and "Cancel" to continue or abort the script

 

  -------------------------- EXAMPLE 2 --------------------------

 

  GUIPAUSE -Message "I Love PowerShell ..." -Title "Another Title"

 

  => same as above with custom message and box title

 

 

 #>

 

######BEGIN PAUSE DEFINITION######

Function GUIPAUSE ($Message = "Click OK to continue or CANCEL to exit script", $Title = "Continue or Cancel") {

 

    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  | Out-Null

    $MsgBox = [System.Windows.Forms.MessageBox]

 

    $Decision = $MsgBox::Show($Message,$Title,"OkCancel", "Information")

    If ($Decision -eq "Cancel") {exit}

 

}

######END PAUSE DEFINITION######

 

 

 

 

 

# SAMPLE CODE TO ILLUSTRATE GUIPAUSE #

 

Write-host "Coucou ! Script launched"

 

GUIPAUSE

 

Write-Host "Continuing ... instruction 001"

 

GUIPAUSE

 

Write-Host "Continuing again ... instruction 002"

 

GUIPAUSE

 

Write-Host "Ending ... Instruction 003"


Viewing all articles
Browse latest Browse all 34890

Trending Articles