Function New-WPFMessageBox { # For examples for use, see my blog: # https://smsagent.wordpress.com/2017/08/24/a-customisable-wpf-messagebox-for-powershell/ # CHANGES # 2017-09-11 - Added some required assemblies in the dynamic parameters to avoid errors when run from the PS console host. # Define Parameters [CmdletBinding()] Param ( # The popup Content [Parameter(Mandatory=$True,Position=0)] [Object]$Content, # The window title [Parameter(Mandatory=$false,Position=1)] [string]$Title, # The buttons to add [Parameter(Mandatory=$false,Position=2)] [ValidateSet('OK','OK-Cancel','Abort-Retry-Ignore','Yes-No-Cancel','Yes-No','Retry-Cancel','Cancel-TryAgain-Continue','None')] [array]$ButtonType = 'OK', # The buttons to add [Parameter(Mandatory=$false,Position=3)] [array]$CustomButtons, # Content font size [Parameter(Mandatory=$false,Position=4)] [int]$ContentFontSize = 14, # Title font size [Parameter(Mandatory=$false,Position=5)] [int]$TitleFontSize = 14, # BorderThickness [Parameter(Mandatory=$false,Position=6)] [int]$BorderThickness = 0, # CornerRadius [Parameter(Mandatory=$false,Position=7)] [int]$CornerRadius = 8, # ShadowDepth [Parameter(Mandatory=$false,Position=8)] [int]$ShadowDepth = 3, # BlurRadius [Parameter(Mandatory=$false,Position=9)] [int]$BlurRadius = 20, # WindowHost [Parameter(Mandatory=$false,Position=10)] [object]$WindowHost, # Timeout in seconds, [Parameter(Mandatory=$false,Position=11)] [int]$Timeout, # Code for Window Loaded event, [Parameter(Mandatory=$false,Position=12)] [scriptblock]$OnLoaded, # Code for Window Closed event, [Parameter(Mandatory=$false,Position=13)] [scriptblock]$OnClosed ) # Dynamically Populated parameters DynamicParam { # Add assemblies for use in PS Console Add-Type -AssemblyName System.Drawing, PresentationCore # ContentBackground $ContentBackground = 'ContentBackground' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.ContentBackground = "White" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ContentBackground, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ContentBackground, $RuntimeParameter) # FontFamily $FontFamily = 'FontFamily' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Drawing.FontFamily]::Families.Name | Select -Skip 1 $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($FontFamily, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($FontFamily, $RuntimeParameter) $PSBoundParameters.FontFamily = "Segoe UI" # TitleFontWeight $TitleFontWeight = 'TitleFontWeight' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Windows.FontWeights] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.TitleFontWeight = "Normal" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($TitleFontWeight, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($TitleFontWeight, $RuntimeParameter) # ContentFontWeight $ContentFontWeight = 'ContentFontWeight' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Windows.FontWeights] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.ContentFontWeight = "Normal" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ContentFontWeight, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ContentFontWeight, $RuntimeParameter) # ContentTextForeground $ContentTextForeground = 'ContentTextForeground' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.ContentTextForeground = "Black" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ContentTextForeground, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ContentTextForeground, $RuntimeParameter) # TitleTextForeground $TitleTextForeground = 'TitleTextForeground' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.TitleTextForeground = "Black" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($TitleTextForeground, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($TitleTextForeground, $RuntimeParameter) # BorderBrush $BorderBrush = 'BorderBrush' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.BorderBrush = "Black" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($BorderBrush, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($BorderBrush, $RuntimeParameter) # TitleBackground $TitleBackground = 'TitleBackground' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.TitleBackground = "White" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($TitleBackground, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($TitleBackground, $RuntimeParameter) # ButtonTextForeground $ButtonTextForeground = 'ButtonTextForeground' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False $AttributeCollection.Add($ParameterAttribute) $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $PSBoundParameters.ButtonTextForeground = "Black" $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ButtonTextForeground, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ButtonTextForeground, $RuntimeParameter) # Sound $Sound = 'Sound' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $False #$ParameterAttribute.Position = 14 $AttributeCollection.Add($ParameterAttribute) $arrSet = (Get-ChildItem "$env:SystemDrive\Windows\Media" -Filter Windows* | Select -ExpandProperty Name).Replace('.wav','') $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $AttributeCollection.Add($ValidateSetAttribute) $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($Sound, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($Sound, $RuntimeParameter) return $RuntimeParameterDictionary } Begin { Add-Type -AssemblyName PresentationFramework } Process { # Define the XAML markup [XML]$Xaml = @" "@ [XML]$ButtonXaml = @"