Displaying Toast Notifications for a Different User when PowerShell Module Updates are Available

A couple of months ago, Josh King presented a session on Using BurntToast to Display Timely Notifications for our June 2018 Mississippi PowerShell User Group virtual meeting. I was previously planning to write something to display balloon notifications in Windows and I learned that they’re now called toast notifications. I also learned that Josh had created a module named BurntToast which performs most of the heavy lifting so I could simply take advantage of it instead of writing my own code.

Note: This blog article is written using Windows 10 version 1803 and Windows PowerShell version 5.1. Your mileage may vary with different operating systems and/or different versions of PowerShell.

First, install the BurntToast module from the PowerShell Gallery.

Install-Module -Name BurntToast -Force

burnttoast1a.png

The BurntToast module contains several functions, but the one I’m interested in is New-BurntToastNotification.

Get-Command -Module BurntToast

burnttoast2a.png

A simple test notification revealed that notifications can’t be displayed for a different user. I log into my computer as a standard user and then run PowerShell elevated as a different user account.

New-BurntToastNotification -Text 'There are new PowerShell module updates available.'

burnttoast3a.png

There didn’t seem to be an option built into the BurntToast module to work around this problem so I resorted to using the Runas.exe command from within PowerShell to display the test notification for a different user.

The first time Runas.exe is used with the Savecred parameter, it prompts for the specified user’s password and saves it in credential manager.

runas.exe /user:mikefrobbins\user01 /savecred powershell.exe

burnttoast4a.png

From that point forward, it will use the saved credentials for the specified user as long as they’re valid.

runas.exe /user:mydomain\mrobbins /savecred "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -Command New-BurntToastNotification -Text 'There are new PowerShell module updates available.'"

burnttoast5a.png

Success! The previous command displays the following toast notification.

burnttoast6a.png

While using the Runas.exe command achieves the desired result, there has to be a PowerShell way to accomplish this task and indeed there is.

$UserCred = Get-Credential
Start-Process -FilePath "$env:windir\System32\WindowsPowerShell\v1.0\powershell.exe" -ArgumentList "-NoProfile -Command New-BurntToastNotification -Text 'There are new PowerShell module updates available.'" -WindowStyle Hidden -Credential $UserCred

I installed Joel Bennett’s BetterCredentials PowerShell module from the PowerShell Gallery so I could easily read credentials from credential manager instead of being prompted for them or storing them in a variable or file.

Install-Module -Name BetterCredentials -Force -AllowClobber

burnttoast8a.png

If you haven’t already, store the credentials in credential manager for the user who is logged into Windows.

Get-Credential -Store

burnttoast9a.png

Now to wire up the code to perform a check to determine if any PowerShell module updates are available. I’ll simply use the Find-MrModuleUpdate function that I’ve previously written which is part of my MrToolkit PowerShell module.

Install-Module -Name MrToolkit -Force

burnttoast7a.png

Finally, I’m going to embed the following code into my PowerShell profile to perform the check and display the toast notification for any/all logged in user(s) if there are any PowerShell module updates available. You also have the option of setting it up as a scheduled task or job instead of adding it to your PowerShell profile.

Start-Job {
if (-not(Get-Module -Name MrToolkit -ListAvailable)) {
Install-Module -Name MrModule -Force
}
if (-not(Get-Module -Name BurntToast -ListAvailable)) {
Install-Module -Name BurntToast -Force
}

if (Find-MrModuleUpdate) {
Get-CimInstance -ClassName Win32_ComputerSystem -Property UserName |
ForEach-Object {
Start-Process -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ArgumentList "-NoProfile -Command New-BurntToastNotification -Text 'There are new PowerShell module updates available.'" -WindowStyle Hidden -Credential (Get-Credential -UserName $_.UserName -Store)
}
}
} | Out-Null

It’s being run as a job because it’s not the fastest thing in the world to check each and every module for updates.

This isn’t the type of thing you want running every time you open PowerShell so I’ve specifically added it to the all users, current host profile for the Windows PowerShell console which is located in the following location: C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1. I’ve also added logic to test for Internet connectivity and only have it run the first time I open the PowerShell console each Tuesday.

if (Test-NetConnection -ComputerName bing.com -Port 80 -InformationLevel Quiet -ErrorAction SilentlyContinue -WarningAction SilentlyContinue) {
$Date = Get-Date

if (($Date).DayOfWeek -eq 'Tuesday') {

$ModuleLUCPath = "$env:ProgramFiles\WindowsPowerShell\Configuration\moduleupdates-lastchecked.txt"

$ModuleLastUpdateCheck = (Get-ChildItem -Path $ModuleLUCPath -ErrorAction SilentlyContinue).LastWriteTime

if ($ModuleLastUpdateCheck.Date -ne $Date.Date) {

if ((New-Object -TypeName System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {

New-Item -Path $ModuleLUCPath -ItemType File -Force | Out-Null

Start-Job {
if (-not(Get-Module -Name MrToolkit -ListAvailable)){
Install-Module -Name MrModule -Force
}
if (-not(Get-Module -Name BurntToast -ListAvailable)){
Install-Module -Name BurntToast -Force
}

if (Find-MrModuleUpdate) {
Get-CimInstance -ClassName Win32_ComputerSystem -Property UserName |
ForEach-Object {
Start-Process -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ArgumentList "-NoProfile -Command New-BurntToastNotification -Text 'There are new PowerShell module updates available.'" -WindowStyle Hidden -Credential (Get-Credential -UserName $_.UserName -Store)
}
}
} | Out-Null

}
else {
Write-Warning -Message 'Aborting due to PowerShell not being run elevated as a local admin!'
}

}

}

}

I’ve gone down a rabbit hole and jumped through some hoops in this blog article to display the notifications because of running PowerShell as a different user than I’m logged into Windows as, but I’ve learned quite a few things in the process that I’m sure will translate to other future tasks.

ยต