Last active
May 2, 2017 08:22
-
-
Save torgro/820cd3de08c34705a5da4f36d6477db2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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