Guest User

Untitled

a guest
Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. function Show-ToastNoficiation
  2. {
  3. <#
  4. .Synopsis
  5. Displays a Toast notification in Windows 10 with PowerShell
  6. .DESCRIPTION
  7. Displays a Toast notification in Windows 10 with PowerShell
  8. .EXAMPLE
  9. Show-ToastNotification -Title 'Script completed' -Body "Your script located at $Path completed"
  10. .EXAMPLE
  11. Show-ToastNotification -Title 'Script completed' -Body "Your script located at $Path completed" -Icon C:\Temp\Logo.png
  12. .INPUTS
  13. System.String
  14. .OUTPUTS
  15. None
  16. .NOTES
  17. (c) 2017 Shawn Esterman. All rights reserved.
  18.  
  19. Follow me on Twitter @ShawnDotTweet
  20. I'll try and post more stuff on github.com/ShawnEsterman
  21. #>
  22.  
  23. [CmdletBinding()]
  24. Param (
  25. [System.String] $Title,
  26. [System.String] $Body,
  27. [System.String] $Icon = "$PSScriptRoot\assets\Powershell_avatar.ico",
  28. [System.String] $Tag = (Get-Random -Minimum 0 -Maximum 99999),
  29. [System.String] $Group = 'poshToast',
  30. [System.String] $Notifier = 'ToastNotificationManager'
  31. )
  32.  
  33. $Template = @"
  34. <toast displayTimestamp="$(Get-Date -Format u)">
  35. <visual>
  36. <binding template="ToastGeneric">
  37. <text hint-maxLines="1">$Title</text>
  38. <text>$Body</text>
  39. <text placement="attribution">via PowerShell</text>
  40. $(if (Test-Path $Icon) {"<image placement=`"appLogoOverride`" hint-crop=`"none`" src=`"$Icon`"/>"})
  41. </binding>
  42. </visual>
  43. </toast>
  44. "@
  45.  
  46. $XmlDocument = New-Object Windows.Data.Xml.Dom.XmlDocument
  47. $XmlDocument.LoadXml($Template)
  48.  
  49. $ToastNotification = [Windows.UI.Notifications.ToastNotification]::new($XmlDocument)
  50. $ToastNotification.Tag = $Tag
  51. $ToastNotification.Group = $Group
  52. $ToastNotification.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)
  53.  
  54. $ToastNotifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($Notifier)
  55. $ToastNotifier.Show($ToastNotification)
  56. }
Add Comment
Please, Sign In to add comment