sam_cayze

Untitled

Jan 29th, 2018
798
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### SAM CAYZE ###
  2. ### BUT ALL MY CREDIT GOES TO TREVOR JONES, WHO MADE THIS AWESOME FUNCTION
  3. # https://smsagent.wordpress.com/2017/08/24/a-customisable-wpf-messagebox-for-powershell/
  4.  
  5.  
  6. Add-Type -AssemblyName PresentationFramework
  7. Add-Type -AssemblyName PresentationCore
  8. Add-Type -AssemblyName WindowsBase
  9.  
  10.  
  11. Function New-WPFMessageBox {
  12.  
  13.     # CREDIT TO Trevor Jones:
  14.     # https://smsagent.wordpress.com/2017/08/24/a-customisable-wpf-messagebox-for-powershell/
  15.    
  16.     # CHANGES
  17.     # 2017-09-11 - Added some required assemblies in the dynamic parameters to avoid errors when run from the PS console host.
  18.    
  19.     # Define Parameters
  20.     [CmdletBinding()]
  21.     Param
  22.     (
  23.         # The popup Content
  24.         [Parameter(Mandatory = $True, Position = 0)]
  25.         [Object]$Content,
  26.  
  27.         # The window title
  28.         [Parameter(Mandatory = $false, Position = 1)]
  29.         [string]$Title,
  30.  
  31.         # The buttons to add
  32.         [Parameter(Mandatory = $false, Position = 2)]
  33.         [ValidateSet('OK', 'OK-Cancel', 'Abort-Retry-Ignore', 'Yes-No-Cancel', 'Yes-No', 'Retry-Cancel', 'Cancel-TryAgain-Continue', 'None')]
  34.         [array]$ButtonType = 'OK',
  35.  
  36.         # The buttons to add
  37.         [Parameter(Mandatory = $false, Position = 3)]
  38.         [array]$CustomButtons,
  39.  
  40.         # Content font size
  41.         [Parameter(Mandatory = $false, Position = 4)]
  42.         [int]$ContentFontSize = 14,
  43.  
  44.         # Title font size
  45.         [Parameter(Mandatory = $false, Position = 5)]
  46.         [int]$TitleFontSize = 14,
  47.  
  48.         # BorderThickness
  49.         [Parameter(Mandatory = $false, Position = 6)]
  50.         [int]$BorderThickness = 0,
  51.  
  52.         # CornerRadius
  53.         [Parameter(Mandatory = $false, Position = 7)]
  54.         [int]$CornerRadius = 8,
  55.  
  56.         # ShadowDepth
  57.         [Parameter(Mandatory = $false, Position = 8)]
  58.         [int]$ShadowDepth = 3,
  59.  
  60.         # BlurRadius
  61.         [Parameter(Mandatory = $false, Position = 9)]
  62.         [int]$BlurRadius = 20,
  63.  
  64.         # WindowHost
  65.         [Parameter(Mandatory = $false, Position = 10)]
  66.         [object]$WindowHost,
  67.  
  68.         # Timeout in seconds,
  69.         [Parameter(Mandatory = $false, Position = 11)]
  70.         [int]$Timeout,
  71.  
  72.         # Code for Window Loaded event,
  73.         [Parameter(Mandatory = $false, Position = 12)]
  74.         [scriptblock]$OnLoaded,
  75.  
  76.         # Code for Window Closed event,
  77.         [Parameter(Mandatory = $false, Position = 13)]
  78.         [scriptblock]$OnClosed
  79.  
  80.     )
  81.  
  82.     # Dynamically Populated parameters
  83.     DynamicParam {
  84.        
  85.         # Add assemblies for use in PS Console
  86.         Add-Type -AssemblyName System.Drawing, PresentationCore
  87.        
  88.         # ContentBackground
  89.         $ContentBackground = 'ContentBackground'
  90.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  91.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  92.         $ParameterAttribute.Mandatory = $False
  93.         $AttributeCollection.Add($ParameterAttribute)
  94.         $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
  95.         $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  96.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  97.         $AttributeCollection.Add($ValidateSetAttribute)
  98.         $PSBoundParameters.ContentBackground = "White"
  99.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ContentBackground, [string], $AttributeCollection)
  100.         $RuntimeParameterDictionary.Add($ContentBackground, $RuntimeParameter)
  101.        
  102.  
  103.         # FontFamily
  104.         $FontFamily = 'FontFamily'
  105.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  106.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  107.         $ParameterAttribute.Mandatory = $False
  108.         $AttributeCollection.Add($ParameterAttribute)  
  109.         $arrSet = [System.Drawing.FontFamily]::Families | Select -ExpandProperty Name
  110.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
  111.         $AttributeCollection.Add($ValidateSetAttribute)
  112.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($FontFamily, [string], $AttributeCollection)
  113.         $RuntimeParameterDictionary.Add($FontFamily, $RuntimeParameter)
  114.         $PSBoundParameters.FontFamily = "Segui"
  115.  
  116.         # TitleFontWeight
  117.         $TitleFontWeight = 'TitleFontWeight'
  118.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  119.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  120.         $ParameterAttribute.Mandatory = $False
  121.         $AttributeCollection.Add($ParameterAttribute)
  122.         $arrSet = [System.Windows.FontWeights] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  123.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  124.         $AttributeCollection.Add($ValidateSetAttribute)
  125.         $PSBoundParameters.TitleFontWeight = "Normal"
  126.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($TitleFontWeight, [string], $AttributeCollection)
  127.         $RuntimeParameterDictionary.Add($TitleFontWeight, $RuntimeParameter)
  128.  
  129.         # ContentFontWeight
  130.         $ContentFontWeight = 'ContentFontWeight'
  131.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  132.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  133.         $ParameterAttribute.Mandatory = $False
  134.         $AttributeCollection.Add($ParameterAttribute)
  135.         $arrSet = [System.Windows.FontWeights] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  136.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  137.         $AttributeCollection.Add($ValidateSetAttribute)
  138.         $PSBoundParameters.ContentFontWeight = "Normal"
  139.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ContentFontWeight, [string], $AttributeCollection)
  140.         $RuntimeParameterDictionary.Add($ContentFontWeight, $RuntimeParameter)
  141.        
  142.  
  143.         # ContentTextForeground
  144.         $ContentTextForeground = 'ContentTextForeground'
  145.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  146.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  147.         $ParameterAttribute.Mandatory = $False
  148.         $AttributeCollection.Add($ParameterAttribute)
  149.         $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  150.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  151.         $AttributeCollection.Add($ValidateSetAttribute)
  152.         $PSBoundParameters.ContentTextForeground = "Black"
  153.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ContentTextForeground, [string], $AttributeCollection)
  154.         $RuntimeParameterDictionary.Add($ContentTextForeground, $RuntimeParameter)
  155.  
  156.         # TitleTextForeground
  157.         $TitleTextForeground = 'TitleTextForeground'
  158.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  159.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  160.         $ParameterAttribute.Mandatory = $False
  161.         $AttributeCollection.Add($ParameterAttribute)
  162.         $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  163.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  164.         $AttributeCollection.Add($ValidateSetAttribute)
  165.         $PSBoundParameters.TitleTextForeground = "Black"
  166.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($TitleTextForeground, [string], $AttributeCollection)
  167.         $RuntimeParameterDictionary.Add($TitleTextForeground, $RuntimeParameter)
  168.  
  169.         # BorderBrush
  170.         $BorderBrush = 'BorderBrush'
  171.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  172.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  173.         $ParameterAttribute.Mandatory = $False
  174.         $AttributeCollection.Add($ParameterAttribute)
  175.         $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  176.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  177.         $AttributeCollection.Add($ValidateSetAttribute)
  178.         $PSBoundParameters.BorderBrush = "Black"
  179.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($BorderBrush, [string], $AttributeCollection)
  180.         $RuntimeParameterDictionary.Add($BorderBrush, $RuntimeParameter)
  181.  
  182.  
  183.         # TitleBackground
  184.         $TitleBackground = 'TitleBackground'
  185.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  186.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  187.         $ParameterAttribute.Mandatory = $False
  188.         $AttributeCollection.Add($ParameterAttribute)
  189.         $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  190.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  191.         $AttributeCollection.Add($ValidateSetAttribute)
  192.         $PSBoundParameters.TitleBackground = "White"
  193.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($TitleBackground, [string], $AttributeCollection)
  194.         $RuntimeParameterDictionary.Add($TitleBackground, $RuntimeParameter)
  195.  
  196.         # ButtonTextForeground
  197.         $ButtonTextForeground = 'ButtonTextForeground'
  198.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  199.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  200.         $ParameterAttribute.Mandatory = $False
  201.         $AttributeCollection.Add($ParameterAttribute)
  202.         $arrSet = [System.Drawing.Brushes] | Get-Member -Static -MemberType Property | Select -ExpandProperty Name
  203.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  204.         $AttributeCollection.Add($ValidateSetAttribute)
  205.         $PSBoundParameters.ButtonTextForeground = "Black"
  206.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ButtonTextForeground, [string], $AttributeCollection)
  207.         $RuntimeParameterDictionary.Add($ButtonTextForeground, $RuntimeParameter)
  208.  
  209.         #### EDIT BY SAM - NOT THE AUTHOR - I COMMENTED THIS OUT AS IT WAS THROWING ERRORS, AND I DIDN'T NEED IT ##########
  210.  
  211.         # Sound
  212.         #$Sound = 'Sound'
  213.         #$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  214.         #$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  215.         #$ParameterAttribute.Mandatory = $False
  216.         #$ParameterAttribute.Position = 14
  217.         #$AttributeCollection.Add($ParameterAttribute)
  218.         #$arrSet = (Get-ChildItem "$env:SystemDrive\Windows\Media" -Filter Windows* | Select -ExpandProperty Name).Replace('.wav', '')
  219.         #$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)    
  220.         #$AttributeCollection.Add($ValidateSetAttribute)
  221.         #$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($Sound, [string], $AttributeCollection)
  222.         #$RuntimeParameterDictionary.Add($Sound, $RuntimeParameter)
  223.  
  224.         return $RuntimeParameterDictionary
  225.     }
  226.  
  227.     Begin {
  228.         Add-Type -AssemblyName PresentationFramework
  229.     }
  230.    
  231.     Process {
  232.  
  233.         # Define the XAML markup
  234.         [XML]$Xaml = @"
  235. <Window
  236.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  237.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  238.        x:Name="Window" Title="" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="True" Background="Transparent" Opacity="1">
  239.    <Window.Resources>
  240.        <Style TargetType="{x:Type Button}">
  241.            <Setter Property="Template">
  242.                <Setter.Value>
  243.                    <ControlTemplate TargetType="Button">
  244.                        <Border>
  245.                            <Grid Background="{TemplateBinding Background}">
  246.                                <ContentPresenter />
  247.                            </Grid>
  248.                        </Border>
  249.                    </ControlTemplate>
  250.                </Setter.Value>
  251.            </Setter>
  252.        </Style>
  253.    </Window.Resources>
  254.    <Border x:Name="MainBorder" Margin="10" CornerRadius="$CornerRadius" BorderThickness="$BorderThickness" BorderBrush="$($PSBoundParameters.BorderBrush)" Padding="0" >
  255.        <Border.Effect>
  256.            <DropShadowEffect x:Name="DSE" Color="Black" Direction="270" BlurRadius="$BlurRadius" ShadowDepth="$ShadowDepth" Opacity="0.6" />
  257.        </Border.Effect>
  258.        <Border.Triggers>
  259.            <EventTrigger RoutedEvent="Window.Loaded">
  260.                <BeginStoryboard>
  261.                    <Storyboard>
  262.                        <DoubleAnimation Storyboard.TargetName="DSE" Storyboard.TargetProperty="ShadowDepth" From="0" To="$ShadowDepth" Duration="0:0:1" AutoReverse="False" />
  263.                        <DoubleAnimation Storyboard.TargetName="DSE" Storyboard.TargetProperty="BlurRadius" From="0" To="$BlurRadius" Duration="0:0:1" AutoReverse="False" />
  264.                    </Storyboard>
  265.                </BeginStoryboard>
  266.            </EventTrigger>
  267.        </Border.Triggers>
  268.        <Grid >
  269.            <Border Name="Mask" CornerRadius="$CornerRadius" Background="$($PSBoundParameters.ContentBackground)" />
  270.            <Grid x:Name="Grid" Background="$($PSBoundParameters.ContentBackground)">
  271.                <Grid.OpacityMask>
  272.                    <VisualBrush Visual="{Binding ElementName=Mask}"/>
  273.                </Grid.OpacityMask>
  274.                <StackPanel Name="StackPanel" >                  
  275.                    <TextBox Name="TitleBar" IsReadOnly="True" IsHitTestVisible="False" Text="$Title" Padding="10" FontFamily="$($PSBoundParameters.FontFamily)" FontSize="$TitleFontSize" Foreground="$($PSBoundParameters.TitleTextForeground)" FontWeight="$($PSBoundParameters.TitleFontWeight)" Background="$($PSBoundParameters.TitleBackground)" HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="Auto" HorizontalContentAlignment="Center" BorderThickness="0"/>
  276.                    <DockPanel Name="ContentHost" Margin="0,10,0,10"  >
  277.                    </DockPanel>
  278.                    <DockPanel Name="ButtonHost" LastChildFill="False" HorizontalAlignment="Center" >
  279.                    </DockPanel>
  280.                </StackPanel>
  281.            </Grid>
  282.        </Grid>
  283.    </Border>
  284. </Window>
  285. "@
  286.  
  287.         [XML]$ButtonXaml = @"
  288. <Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="Auto" Height="30" FontFamily="Segui" FontSize="16" Background="Transparent" Foreground="White" BorderThickness="1" Margin="10" Padding="20,0,20,0" HorizontalAlignment="Right" Cursor="Hand"/>
  289. "@
  290.  
  291.         [XML]$ButtonTextXaml = @"
  292. <TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" FontFamily="$($PSBoundParameters.FontFamily)" FontSize="16" Background="Transparent" Foreground="$($PSBoundParameters.ButtonTextForeground)" Padding="20,5,20,5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  293. "@
  294.  
  295.         [XML]$ContentTextXaml = @"
  296. <TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Text="$Content" Foreground="$($PSBoundParameters.ContentTextForeground)" DockPanel.Dock="Right" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="$($PSBoundParameters.FontFamily)" FontSize="$ContentFontSize" FontWeight="$($PSBoundParameters.ContentFontWeight)" TextWrapping="Wrap" Height="Auto" MaxWidth="500" MinWidth="50" Padding="10"/>
  297. "@
  298.  
  299.         # Load the window from XAML
  300.        
  301.         $Window = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml))
  302.         $window.topmost = $true
  303.  
  304.         # Custom function to add a button
  305.         Function Add-Button {
  306.             Param($Content)
  307.             $Button = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ButtonXaml))
  308.             $ButtonText = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ButtonTextXaml))
  309.             $ButtonText.Text = "$Content"
  310.             $Button.Content = $ButtonText
  311.             $Button.Add_MouseEnter( {
  312.                     $This.Content.FontSize = "17"
  313.                 })
  314.             $Button.Add_MouseLeave( {
  315.                     $This.Content.FontSize = "16"
  316.                 })
  317.             $Button.Add_Click( {
  318.                     New-Variable -Name WPFMessageBoxOutput -Value $($This.Content.Text) -Option ReadOnly -Scope Script -Force
  319.                     $Window.Close()
  320.                 })
  321.             $Window.FindName('ButtonHost').AddChild($Button)
  322.         }
  323.  
  324.         # Add buttons
  325.         If ($ButtonType -eq "OK") {
  326.             Add-Button -Content "OK"
  327.         }
  328.  
  329.         If ($ButtonType -eq "OK-Cancel") {
  330.             Add-Button -Content "OK"
  331.             Add-Button -Content "Cancel"
  332.         }
  333.  
  334.         If ($ButtonType -eq "Abort-Retry-Ignore") {
  335.             Add-Button -Content "Abort"
  336.             Add-Button -Content "Retry"
  337.             Add-Button -Content "Ignore"
  338.         }
  339.  
  340.         If ($ButtonType -eq "Yes-No-Cancel") {
  341.             Add-Button -Content "Yes"
  342.             Add-Button -Content "No"
  343.             Add-Button -Content "Cancel"
  344.         }
  345.  
  346.         If ($ButtonType -eq "Yes-No") {
  347.             Add-Button -Content "Yes"
  348.             Add-Button -Content "No"
  349.         }
  350.  
  351.         If ($ButtonType -eq "Retry-Cancel") {
  352.             Add-Button -Content "Retry"
  353.             Add-Button -Content "Cancel"
  354.         }
  355.  
  356.         If ($ButtonType -eq "Cancel-TryAgain-Continue") {
  357.             Add-Button -Content "Cancel"
  358.             Add-Button -Content "TryAgain"
  359.             Add-Button -Content "Continue"
  360.         }
  361.  
  362.         If ($ButtonType -eq "None" -and $CustomButtons) {
  363.             Foreach ($CustomButton in $CustomButtons) {
  364.                 Add-Button -Content "$CustomButton"
  365.             }
  366.         }
  367.  
  368.         # Remove the title bar if no title is provided
  369.         If ($Title -eq "") {
  370.             $TitleBar = $Window.FindName('TitleBar')
  371.             $Window.FindName('StackPanel').Children.Remove($TitleBar)
  372.         }
  373.  
  374.         # Add the Content
  375.         If ($Content -is [String]) {
  376.             # Replace double quotes with single to avoid quote issues in strings
  377.             If ($Content -match '"') {
  378.                 $Content = $Content.Replace('"', "'")
  379.             }
  380.        
  381.             # Use a text box for a string value...
  382.             $ContentTextBox = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ContentTextXaml))
  383.             $Window.FindName('ContentHost').AddChild($ContentTextBox)
  384.         }
  385.         Else {
  386.             # ...or add a WPF element as a child
  387.             Try {
  388.                 $Window.FindName('ContentHost').AddChild($Content)
  389.             }
  390.             Catch {
  391.                 $_
  392.             }        
  393.         }
  394.  
  395.         # Enable window to move when dragged
  396.         $Window.FindName('Grid').Add_MouseLeftButtonDown( {
  397.                 $Window.DragMove()
  398.             })
  399.  
  400.         # Activate the window on loading
  401.         If ($OnLoaded) {
  402.             $Window.Add_Loaded( {
  403.                     $This.Activate()
  404.                     Invoke-Command $OnLoaded
  405.                 })
  406.         }
  407.         Else {
  408.             $Window.Add_Loaded( {
  409.                     $This.Activate()
  410.                 })
  411.         }
  412.    
  413.  
  414.         # Stop the dispatcher timer if exists
  415.         If ($OnClosed) {
  416.             $Window.Add_Closed( {
  417.                     If ($DispatcherTimer) {
  418.                         $DispatcherTimer.Stop()
  419.                     }
  420.                     Invoke-Command $OnClosed
  421.                 })
  422.         }
  423.         Else {
  424.             $Window.Add_Closed( {
  425.                     If ($DispatcherTimer) {
  426.                         $DispatcherTimer.Stop()
  427.                     }
  428.                 })
  429.         }
  430.    
  431.  
  432.         # If a window host is provided assign it as the owner
  433.         If ($WindowHost) {
  434.             $Window.Owner = $WindowHost
  435.             $Window.WindowStartupLocation = "CenterOwner"
  436.         }
  437.  
  438.         # If a timeout value is provided, use a dispatcher timer to close the window when timeout is reached
  439.    
  440.         If ($Timeout) {
  441.             $Stopwatch = New-object System.Diagnostics.Stopwatch
  442.             $TimerCode = {
  443.                 If ($Stopwatch.Elapsed.TotalSeconds -ge $Timeout) {
  444.                     $Stopwatch.Stop()
  445.                     $Window.Close()
  446.                     $TimeoutReached = 1
  447.                 }
  448.             }
  449.             $DispatcherTimer = New-Object -TypeName System.Windows.Threading.DispatcherTimer
  450.             $DispatcherTimer.Interval = [TimeSpan]::FromSeconds(1)
  451.             $DispatcherTimer.Add_Tick($TimerCode)
  452.             $Stopwatch.Start()
  453.             $DispatcherTimer.Start()
  454.         }
  455.  
  456.         # Play a sound
  457.         If ($($PSBoundParameters.Sound)) {
  458.             $SoundFile = "$env:SystemDrive\Windows\Media\$($PSBoundParameters.Sound).wav"
  459.             $SoundPlayer = New-Object System.Media.SoundPlayer -ArgumentList $SoundFile
  460.             $SoundPlayer.Add_LoadCompleted( {
  461.                     $This.Play()
  462.                     $This.Dispose()
  463.                 })
  464.             $SoundPlayer.LoadAsync()
  465.         }
  466.  
  467.         # Display the window
  468.         $null = $window.Dispatcher.InvokeAsync{$window.ShowDialog()}.Wait()
  469.  
  470.     }
  471. }
  472.  
  473. ###########  END OF FUNCTION  #################
  474.  
  475.  
  476. $Params = @{
  477.     Title = "Contoso IT Software Installation"
  478.     TitleFontSize = 20
  479.     ButtonType = 'None'
  480.     CustomButtons = "Proceed with Installation","Ask me again in 10 minutes"
  481.     TitleBackground = 'SteelBlue'
  482.     }
  483.  
  484. $Image = New-Object System.Windows.Controls.Image
  485. $Image.Source = "http://solopracticeuniversity.com/files/2017/04/microsoft-office-365-logo.png"
  486. $Image.Height = 136.4
  487. $Image.Width = 201.2
  488. $Image.Margin = 5
  489.  
  490. $TextBlock = New-Object System.Windows.Controls.TextBlock
  491. $TextBlock.Text = "Message from Contoso IT: `n
  492. Do you wish to proceed with the installation of Office 365/2016?
  493. Please close all Office applications before continuing. `n
  494. The installation will take around 15-30 minutes."
  495. $TextBlock.Padding = 12
  496. $TextBlock.FontFamily = "Verdana"
  497. $TextBlock.FontSize = 14
  498. $TextBlock.VerticalAlignment = "Center"
  499.  
  500. $StackPanel = New-Object System.Windows.Controls.StackPanel
  501. $StackPanel.Orientation = "Horizontal"
  502. $StackPanel.AddChild($Image)
  503. $StackPanel.AddChild($TextBlock)
  504.  
  505.  
  506.  
  507. ####### DISPLAY MESSAGE ##########
  508. New-WPFMessageBox @Params -Content $StackPanel
  509.  
  510.  
  511. ####### PROCESS RESULTS ##########
  512.     If ($WPFMessageBoxOutput -eq "Proceed with Installation")
  513.     {
  514.         ## TESTING CODE ###
  515.         # Write-Output "Proceed with Installation Clicked"
  516.         $CustomExitCode = 0
  517.     }
  518.     ElseIf ($WPFMessageBoxOutput -eq "Ask Me Again in 10 Minutes")
  519.     {
  520.         ## TESTING CODE ###
  521.         # Write-Output "Not now Clicked"
  522.         $CustomExitCode = 1
  523.     }
  524.     Else
  525.     {
  526.     }
  527.  
  528. ####### FINAL OUTPUT ##########
  529.     # Write-Output $CustomExitCode
  530.     EXIT $CustomExitCode
RAW Paste Data