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