Functions/Show-MessageBox.ps1

<#
.Synopsis
   Short description
.DESCRIPTION
   Long description
.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
#>

function Show-MessageBox ()
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([bool])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true)]
        [string] $Message,
        
        [Parameter(Mandatory=$false)]
        [string] $WindowTitle = '4PS construct maintenance',
        
        [Parameter(Mandatory=$false)]
        [ValidateSet('None', 'Question', 'Error', 'Warning', 'Information')]
        [string] $Icon = 'Information',
        
        # Error message shown when user used No or Cancel button
        [Parameter(Mandatory=$false)]
        [string] $ErrorMsg = 'A not specified error occurred',

        [Parameter(Mandatory=$false)]
        [switch] $Throw = $false,

        [Parameter(Mandatory=$false)]
        [ValidateSet('InShell', 'MessageBox')]
        [string] $MessageType = 'MessageBox',

        # Only supported for MessageBox, InShell is always YesNo
        [Parameter(Mandatory=$false)]
        [ValidateSet('OK', 'OKCancel', 'YesNo', 'YesNoCancel')]
        [string] $Button = 'OK'
    )

    Begin
    {
        if ($MessageType -eq 'MessageBox') {
            
            Add-Type -AssemblyName PresentationFramework
        }
    }
    Process
    {
        
        if ($MessageType -eq 'MessageBox') {

            $Results = [System.Windows.MessageBox]::Show($Message, $WindowTitle, $Button ,$Icon)

            If (($Results -eq 'No' -or $Results -eq 'Cancel')) {
            
                [System.Windows.MessageBox]::Show($ErrorMsg, 'Script could not complete', 'OK','Error')
            
                if ($Throw) {

                    throw $ErrorMsg
                }
            }

            return

        } # End If messagebox

        if ($MessageType -eq 'InShell') {

            $Results = Read-Host "Yes or No"

            While ("yes","y","no","n" -notcontains $Results) {
                
                $Results = Read-Host "Please answer Yes or No"
            }
            
            If ($Results -eq "no" -or $Results -eq "n") {
              
                if ($Throw) {

                    throw $ErrorMsg
                }

            } Else {
                
                Write-Host "Ok. Proceeding to next step..."
                return
            }

        } # End If InShell
    }
    End
    {
        
    }
}


Export-ModuleMember -Function Show-MessageBox

function UserValidation ()
{

    $answer = Read-Host "Yes or No"

    while ("yes","y","no","n" -notcontains $Answer)
    {
        $Answer = Read-Host "Please answer Yes or No"
    }
    if ($Answer -eq "no" -or $Answer -eq "n") {

        Write-Host "Not proceeding..."
        throw "User cancelled stop service, process or applicationpool action." 

    } else {
        Write-Host "Ok. Proceeding to next step..."
        return
    }
}