If you’d like to show a default MessageBox with some buttons for the user to click, try this function:

function Show-MessageBox
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [String]
    $Text,
    
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [String]
    $Caption,
    
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [Windows.MessageBoxButton]
    $Button,
    
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [Windows.MessageBoxImage]
    $Icon
    
  )
  
  process
  {
    try
    {
      [System.Windows.MessageBox]::Show($Text, $Caption, $Button, $Icon)
    }
    catch
    {
      Write-Warning "Error occured: $_"
    }
  }
}

And here is how you’d use it:

 
PS> Show-MessageBox -Text 'Do you want to reboot now?' -Caption Reboot -Button YesNoCancel -Icon Exclamatio 
 

Twitter This Tip! ReTweet this Tip!

Anonymous
Parents
No Data
Comment
  • Hi All,

    This is great to get the box to appear and click the desired button.

    My question is ... Where do we enter the if statement to reboot the computer (if that is what we want to do)?

    I see within the function there is a $button variable and I am guessing this is what the -Button YesNoCancel is assigned to, and selecting a button it is written to the host ... I am guessing this is the output.

    Therefore I am wondering do we want to then pipe the output to an if statement EG IF Output = yes then Shutdown -f -r -t 0 Else exit or is it there a better way of doing it?

    Show-MessageBox -Text 'Do you want to restart now?' -Caption "DONMAN Setup - Restart Required!" -Button YesNoCancel -Icon Exclamation

Children
No Data