internal/functions/Show-Notification.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function Show-Notification {
    param(
        $Title = "dbatools update",
        $Text = "Version $galleryVersion is now available"
    )
    # ensure the dbatools 'app' exists in registry so that it doesn't immediately disappear from Action Center
    $regpath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings'
    $appid = "dbatools"

    if (!(Test-Path -Path "$regpath\$appid")) {
        Write-Verbose "Adding required registry entry at $("$regpath\$appid")"
        $null = New-Item -Path $regpath -Name $appid
        $null = New-ItemProperty -Path "$regpath\$appid" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD'
    }

    $null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
    $templateType = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText02
    $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($templateType)

    $toastXml = [xml]$template.GetXml()
    $toastTextElements = $toastXml.GetElementsByTagName("text")

    $null = $toastTextElements[0].AppendChild($toastXml.CreateTextNode($Title))
    $null = $toastTextElements[1].AppendChild($toastXml.CreateTextNode($Text))

    $image = $toastXml.GetElementsByTagName("image")
    $base = $module.ModuleBase

    $image.setAttribute("src", "$base\bin\thor.png")
    $image.setAttribute("alt", "thor")

    $xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    $xml.LoadXml($toastXml.OuterXml)

    $toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
    $toast.Tag = "PowerShell"
    $toast.Group = "dbatools"
    $null = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("dbatools").Show($toast)
}