Send notifications through powershell scripts

Hello, everyone!
How are they?

Do you have any recommendations for giving feedback to the user when executing scripts?

I tested some features like building a notification with .NET library features, and or BurntToast. But I was unsuccessful.

BurntToast even worked during the tests but only when sent individually, when included in a larger script it does not notify.

If you have suggestions or recommendations I would appreciate it.

Comments

  • COOLNETAU
    COOLNETAU Member Posts: 58 ✭✭
    edited October 25

    try this - if you make this standalone script. it will ask for a Message - if you need to add line breaks? "\n" will do it.

    It is not pretty, but works when I need it to

    Function New-WPFDialog {
    <#
    .SYNOPSIS
    This neat little function is based on the one from Brian Posey's Article on Powershell GUIs

    .DESCRIPTION
    I re-factored a bit to return the resulting XaML Reader and controls as a single, named collection.

    .PARAMETER XamlData
    XamlData - A string containing valid XaML data

    .EXAMPLE

    $MyForm = New-WPFDialog -XamlData $XaMLData
    $MyForm.Exit.Add_Click({...})
    $null = $MyForm.UI.Dispatcher.InvokeAsync{$MyForm.UI.ShowDialog()}.Wait()

    .NOTES
    # Example usage with line breaks
    $MessageText = "Line 1`nLine 2`nLine 3"
    New-PopUpWindow -MessageText $MessageText

    .LINK
    http://www.windowsnetworking.com/articles-tutorials/netgeneral/building-powershell-gui-part2.html

    .INPUTS
    XamlData - A string containing valid XaML data

    .OUTPUTS
    a collection of WPF GUI objects.
    #>

    Param(
    [Parameter(Mandatory = $True, HelpMessage = 'XaML Data defining a GUI', Position = 1)]
    [string]$XamlData
    )

    # Add WPF and Windows Forms assemblies
    try {
    Add-Type -AssemblyName PresentationCore, PresentationFramework, WindowsBase, system.windows.forms
    } catch {
    Throw 'Failed to load Windows Presentation Framework assemblies.'
    }

    # Create an XML Object with the XaML data in it
    [xml]$xmlWPF = $XamlData

    # Create the XAML reader using a new XML node reader, UI is the only hard-coded object name here
    Set-Variable -Name XaMLReader -Value @{ 'UI' = ([Windows.Markup.XamlReader]::Load((new-object -TypeName System.Xml.XmlNodeReader -ArgumentList $xmlWPF))) }

    # Create hooks to each named object in the XAML reader
    $Elements = $xmlWPF.SelectNodes('//*[@Name]')
    ForEach ($Element in $Elements) {
    $VarName = $Element.Name
    $VarValue = $XaMLReader.UI.FindName($Element.Name)
    $XaMLReader.Add($VarName, $VarValue)
    }

    return $XaMLReader
    }

    Function New-PopUpWindow {
    param(
    [string]$MessageText = "Default Message"
    )

    # This is the XaML that defines the GUI.
    $WPFXaml = @'
    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Popup" Background="#FF0066CC" Foreground="#FFFFFFFF" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" WindowStyle="None" Padding="20" Margin="0" Topmost="True">
    <Grid>
    <Button Name="OKButton" Content="OK" HorizontalAlignment="Right" Margin="0,0,30,20" VerticalAlignment="Bottom" Width="75" Background="#FF0066CC" BorderBrush="White" Foreground="White" Padding="8,4"/>
    <TextBlock Name="Message" Margin="100,60,100,80" TextWrapping="Wrap" Text="_CONTENT_" FontSize="36"/>
    </Grid>
    </Window>
    '@

    # Replace the placeholder text with the actual message
    $WPFXaml = $WPFXaml -replace "_CONTENT_", $MessageText

    # Build Dialog
    $WPFGui = New-WPFDialog -XamlData $WPFXaml
    $WPFGui.Message.Text = $MessageText
    $WPFGui.OKButton.Add_Click({ $WPFGui.UI.Close() })
    $null = $WPFGui.UI.Dispatcher.InvokeAsync{ $WPFGui.UI.ShowDialog() }.Wait()
    }
    $MessageText = "{[MessageContent]}"
    New-PopUpWindow -MessageText $MessageText

  • marcos.santos
    marcos.santos Member Posts: 5
    edited October 26

    My two problems occurred because I ran the script directly on the system instead of current user, but it worked perfectly, thanks
    COOLNETAU.

    Here is my code that I commented, just for documentation purposes:

    function Notify {

    param (

    [string]$icon,

    [string]$title,

    [string]$message

    )

    New-BurntToastNotification -AppLogo $icon -Text $title, $message

    }

    $ic, $tt, $ms = "icon.ico", "Title", "My message"

    Notify -icon $ic -title $tt -message $ms

  • COOLNETAU
    COOLNETAU Member Posts: 58 ✭✭

    actually new line should be `n i think not \n