Want to become a Windows 10 Toast/Balloon expert – With or without Microsoft Intune

Posted by

When deploying a script to users it might be a thing to inform them about what and when the change has been made. See the screenshots how the toast, balloon or popup message might look like

image

I have an old post regarding toasts but now I thought it might a be a good idea to update this information. So I rewrote the script to be implemented as a function instead, and make it possible to attach an image as well. The toast or popups will only stay for a while defined by short or long, you might test here what is suitable for you. When the toast disappears it will stay in Action center, the popup message will not stay in the Action center.

I use this function when I deploy some script with Intune to make the users aware of what and when things happen. Depending if the script is deploy to user or system context the toast might look different, but the function does take care about this for you. Also if you want to deploy an image or not.

The function is called easily with one of these two commands

ShowToast -Image “https://picsum.photos/150/150?image=1060 ” -ToastTitle “Windows tweaked!” -ToastText “IT Support has deployed new settings for you! Please logout when possible” -ToastDuration short;

ShowToast -ToastTitle “Windows tweaked!” -ToastText “IT Support has deployed new settings for you! Please logout when possible” -ToastDuration long;

 

and the function itself can be found on my public GitHub or here

function ShowToast {
param(
[parameter(Mandatory=$true,Position=2)]
[string] $ToastTitle,
[parameter(Mandatory=$true,Position=3)]
[string] $ToastText,
[parameter(Position=1)]
[string] $Image = $null,
[parameter()]
[ValidateSet(‘long’,’short’)]
[string] $ToastDuration = “long”
)
# Toast overview: https://msdn.microsoft.com/en-us/library/windows/apps/hh779727.aspx
# Toasts templates: https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null

# Define Toast template, w/wo image
$ToastTemplate = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText02
if ($Image.Length -le 0) { $ToastTemplate = [Windows.UI.Notifications.ToastTemplateType]::ToastText02 }

# Download or define a local image. Toast images must have dimensions =< 1024×1024 size =< 200 KB
if ($Image -match “http*”) {
[System.Reflection.Assembly]::LoadWithPartialName(“System.web”) | Out-Null
$Image = [System.Web.HttpUtility]::UrlEncode($Image)
$imglocal = “$($env:TEMP)\ToastImage.png”
Start-BitsTransfer -Destination $imglocal -Source $([System.Web.HttpUtility]::UrlDecode($Image)) -ErrorAction Continue
} else { $imglocal = $Image }

# Define the toast template and create variable for XML manipulation
# Customize the toast title, text, image and duration
$toastXml = [xml] $([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent(`
$ToastTemplate)).GetXml()
$toastXml.GetElementsByTagName(“text”)[0].AppendChild($toastXml.CreateTextNode($ToastTitle)) | Out-Null
$toastXml.GetElementsByTagName(“text”)[1].AppendChild($toastXml.CreateTextNode($ToastText)) | Out-Null
if ($Image.Length -ge 1) { $toastXml.GetElementsByTagName(“image”)[0].SetAttribute(“src”, $imglocal) }
$toastXml.toast.SetAttribute(“duration”, $ToastDuration)

# Convert back to WinRT type
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument; $xml.LoadXml($toastXml.OuterXml);
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)

# Get an unique AppId from start, and enable notification in registry
if ([System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value.ToString() -eq “S-1-5-18”) {
# Popup alternative when running as system. https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx
$wshell = New-Object -ComObject Wscript.Shell
if ($ToastDuration -eq “long”) { $return = $wshell.Popup($ToastText,10,$ToastTitle,0x100) }
else { $return = $wshell.Popup($ToastText,4,$ToastTitle,0x100) }
} else {
$AppID = ((Get-StartApps -Name ‘Windows Powershell’) | Select -First 1).AppId
New-Item “HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$AppID” -Force | Out-Null
Set-ItemProperty “HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$AppID” `
-Name “ShowInActionCenter” -Type Dword -Value “1” -Force | Out-Null
# Create and show the toast, dont forget AppId
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppID).Show($Toast)
}
}

 

Shout out to me if you have any questions or comments

Advertisements

6 comments

  1. Hi, I’m getting following error when executing your script:

    Start-BitsTransfer : Method not found: ‘UInt64
    Microsoft.BackgroundIntelligentTransfer.Management.Interop.JobProgress.get_BytesTotal()’.
    At X:\Temp\ShowToast.ps1:63 char:13
    + $null = Start-BitsTransfer -Destination $imglocal -Source $([Syst …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Start-BitsTransfer], MissingMethodException
    + FullyQualifiedErrorId : System.MissingMethodException,Microsoft.BackgroundIntelligentTransfer.Management.NewBits
    TransferCommand

    Any idea how to solve this? Thanks

    Like

    1. Hi Gyz, sorry to see that you are having problems. But it looks like you having problem on the row: “$null = Start-BitsTransfer -Destination $imglocal -Source $([Sy”.. Must be something wrong with this line, can you email me your script and i will have a look ?

      Like

  2. I was able to figure it out – the issue was SCCM package running in 32bit context – so you need to force it to use 64bit powershell by using this in your program:
    “%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe”

    Liked by 1 person

  3. I am having trouble getting this script to execute as the logged on user of a remote computer – I am not using Intune. Am I missing something very easy because I feel I am.

    Thank you in advance.

    Like

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.