Worklet - Toast Notifications with Branding and Ignores Focus Assist

  • 24 January 2022
  • 3 replies
  • 1047 views

Userlevel 5
Badge +1

Yup. Another Toast Notification post :-) What makes this one unique is how customizable it can be with company branding and links to internal sites, interactive buttons and so on. Once you get the hang of this one you can apply this to multiple scenarios that fit your needs.

  1. Really need to read Martin Bengtsson blog before you get started. https://www.imab.dk/windows-10-toast-notification-script/ 
  2. Download the scripts here https://github.com/imabdk/Toast-Notification-Script
  3. Edit config-toast.xml to your satisfaction or desired outcome.
  4. Create a new worklet and associate to the right groups
  5. Set the Evaluation Code to whatever you want
    exit 1
  6. Set the Remediation Code Payload
    # Scheduled Task Name
    $schdtaskname = 'ToastNotify'

    # Working Directory
    $workdir = 'C:\ProgramData\ToastNotify'
    IF((Test-Path $workdir) -eq $false){mkdir $workdir -Force | Out-Null}
    IF((Test-Path $workdir\Images) -eq $false){mkdir $workdir\Images -Force | Out-Null}

    # Cleanup Prior Scheduled tasks if they exist
    Unregister-ScheduledTask -TaskName "$schdtaskname" -Confirm:$false | Out-Null

    # Build RunToastHidden.cmd
    $cmd = @"
    powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "$workdir\New-ToastNotification.ps1"
    "@
    New-Item -Path "$workdir" -Name "RunToastHidden.cmd" -ItemType "file" -Value $cmd -force | Out-Null

    # Copy Toast Notify Files
    $collection = 'ToastLogoImageDefault.jpg','ToastHeroImageDefault.jpg'
    foreach ($file in $collection){
    IF(!(Test-Path $workdir\$file)){ Copy-Item ".\$file" $workdir\Images | out-null }
    }
    $collection = 'New-ToastNotification.ps1','config-toast.xml','Hidden.vbs'
    foreach ($file in $collection){
    IF((Get-ChildItem $workdir\$file -ErrorAction SilentlyContinue).LastWriteTime -lt (Get-ChildItem $file).LastWriteTime){
    Copy-Item ".\$file" $workdir | out-null
    }
    }

    # Scheduled Task Functions
    Function Schedule-ToastNotify{
    $TaskStartTime = (Get-Date 12pm)
    $SchedService = New-Object -ComObject Schedule.Service
    $SchedService.Connect()
    $Task = $SchedService.NewTask(0)
    $Task.RegistrationInfo.Description = 'ToastNotify'
    $Task.Settings.Enabled = $true
    $Task.Settings.AllowDemandStart = $true
    $Task.Settings.WakeToRun = $true
    $trigger = $Task.triggers.Create(1) # https://docs.microsoft.com/en-us/windows/win32/taskschd/triggercollection-create
    $trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
    $trigger.Enabled = $true
    $action = $Task.Actions.Create(0)
    $action.Path = "wscript"
    $action.Arguments = "`"$workdir\Hidden.vbs`" `"$workdir\RunToastHidden.cmd`""
    $taskFolder = $SchedService.GetFolder('\')
    $taskFolder.RegisterTaskDefinition("$schdtaskname", $Task , 6, 'Users', $null, 4) | out-null
    }

    # Running Scheduled Task Function
    function Run-ToastNotify{
    IF($TaskSchd -eq $true){
    $time = New-ScheduledTaskTrigger -At (Get-Date).AddSeconds(3) -Once
    Set-ScheduledTask -TaskName "$schdtaskname" -Trigger $time | Out-Null
    }
    }

    # Schedule and Run the Scheduled Task
    Schedule-ToastNotify
    Run-ToastNotify
  7. Upload ALL the files to the worklet (config-toast.xml, Hidden.vbs, New-ToastNotification.ps1, ToastHeroImageDefault.jpg, ToastLogoImageDefault.jpg):
  8. Run worklet

 

After the worklet runs the end user should see this. Take note of the notification icon in lower right with the “moon” logo indicating that Focus Assist is on.
 

 

 

Here you can see where the worklet put data on the local drive

 

Troubleshooting

log data for the toast notification is stored under C:\Users\%username%\AppData\Roaming\ToastNotficationScript\

 

 


3 replies

Userlevel 1
Badge

Hi,

We are currently looking for something like this, but I can´t get this worklet to run.

Through Powershell, on my machine, it works, but through Automox it´s not working, and we can´t debug why.

Thanks

Userlevel 5
Badge +1

Sometimes I will drop in this line at the top of the script to get an idea of what is happening when the worklet runs. After the worklet runs I review output of C:\windows\temp\worklet.txt

 

Start-Transcript C:\Windows\Temp\worklet.txt

Another option is try and see what is not happening by evaluating the various parts the script accomplishes

  1. Does it create the directory C:\ProgramData\ToastNotify
  2. Does it create a file C:\ProgramData\ToastNotify\RunToastHidden.cmd
  3. Did these files “config-toast.xml, Hidden.vbs, New-ToastNotification.ps1, ToastHeroImageDefault.jpg, ToastLogoImageDefault.jpg” get copied under C:\ProgramData\ToastNotify
  4. Does a schedule task called ToastNotify exist
  5. Is there a history of the Scheduled Task running
  6. If the scheduled task is running, did any output get generated here C:\Users\%username%\AppData\Roaming\ToastNotficationScript\ for the currently logged in user...
Userlevel 1
Badge

Thanks for the ideas! 

We’ll keep debugging and update here with our final output. 

Reply