Skip to content

Instantly share code, notes, and snippets.

@torgro
Last active May 2, 2017 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save torgro/820cd3de08c34705a5da4f36d6477db2 to your computer and use it in GitHub Desktop.
Save torgro/820cd3de08c34705a5da4f36d6477db2 to your computer and use it in GitHub Desktop.
function Show-Message
{
[cmdletbinding()]
Param(
[Parameter(ValueFromPipeLine)]
$Message
,
[Parameter(ParameterSetName="Win10")]
$DisplayDuration = 10
,
[Parameter(ParameterSetName="Win10")]
[switch]$NotificationArea
,
[Parameter(ParameterSetName="Win10")]
[string]$Tag = "Powershell"
,
[Parameter(ParameterSetName="Win10")]
[string]$Group = "Powershell"
)
Begin
{
if ($NotificationArea)
{
try
{
$null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
}
catch
{
$ex = $_.Exception
Write-Warning -Message "Notification area not supported, $($ex.Message)"
$null = $PSBoundParameters.Remove("NotificationArea")
$NotificationArea = $false
}
}
}
Process
{
if (-not $NotificationArea)
{
$null = [System.Windows.Forms.Messagebox]::Show($Message)
}
if ($NotificationArea)
{
$toastXml = [xml]$template.GetXml()
$null = $toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($Message))
#Convert back to WinRT type
$xml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = $Tag
$toast.Group = $Group
$toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds($DisplayDuration)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Powershell")
$notifier.Show($toast)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment