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
-
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 toFunction 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 $MessageText0 -
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
0 -
actually new line should be `n i think not \n
0
Topics
- All Topics
- 40 Getting started
- 24 Read before posting
- 8 Meet and greet
- 233 General
- 63 News and announcements
- 1 Swag
- 1 Roadmap updates
- 76 Resources
- 12 Knowledge Base
- 14 Webinars
- 1 Shared Script Library
- 2 Blog
- 19 Pro Tips
- 26 Got an idea?
- 3 Atera Academy
- 2 ActionAI
- 1 Copilot
- 132 Remote Monitoring and Management
- 82 Remote Monitoring
- 25 Patch Management
- 103 Professional Services Automation
- 64 Helpdesk
- 17 Billing
- 19 Reporting
- 35 Integrations & add-ons
- 20 Integrations
- 10 Add-ons
- 102 Scripting and automations
- 61 Scripts
- 29 Automations