Skip to main content

Windows - Notify User with Balloon Tip Pop-up

  • October 26, 2020
  • 4 replies
  • 693 views

jack.smith
Forum|alt.badge.img+1

Displays a balloon tip from the task tray for about 8 seconds for currently logged in user. Would invite anyone to help me get this code further! Worklet does use a custom icon file that I include as a file in the worklet.


#message

$msg = 'Hello world.'

$title = 'Company'

$icon = 'C:\Users\Public\Pictures\company.ico'

IF(!(Test-Path "$icon")){Copy-Item company.ico "$icon"}



# Build script that will send message. VBS is used to hide the PowerShell Script from popping up

$vbs = @"

command = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -windowstyle hidden -File C:\ProgramData\Amagent\message.ps1 -Force"

 set shell = CreateObject("WScript.Shell")

 shell.Run command,0

"@

New-Item -Path "c:\ProgramData\Amagent" -Name "message.vbs" -ItemType "file" -Value $vbs -force



$script = @"

`$BalloonTipText = ("$msg")

`$BalloonTipTitle = "$title"

`$BalloonTipTime = 1000

`$BalloonTipIcon = 'None'



Add-Type -AssemblyName System.Windows.Forms

Add-Type -AssemblyName System.Drawing

[Windows.Forms.ToolTipIcon]`$BalloonTipIcon = `$BalloonTipIcon

`$ContextMenu = New-Object System.Windows.Forms.ContextMenu

`$MenuItem = New-Object System.Windows.Forms.MenuItem

        

`$NotifyIcon = New-Object Windows.Forms.NotifyIcon -Property @{

    BalloonTipIcon = `$BalloonTipIcon

    BalloonTipText = `$BalloonTipText

    BalloonTipTitle = `$BalloonTipTitle

    ContextMenu = `$ContextMenu

    Icon = "$icon"

    Text = -join `$BalloonTipText[0..62]

    Visible = `$false

}

`$NotifyIcon.contextMenu.MenuItems.AddRange(`$MenuItem)

`$NotifyIcon.Visible = `$True

`$MenuItem.Text = "Exit"

`$MenuItem.add_Click({

    `$NotifyIcon.Visible = `$true

    `$NotifyIcon.ShowInTaskbar = `$true

})

`$NotifyIcon.ShowBalloonTip(`$BalloonTipTime)

"@

New-Item -Path "c:\ProgramData\Amagent" -Name "message.ps1" -ItemType "file" -Value $script -force



# This will create a scheduled task

$SchedService = New-Object -ComObject Schedule.Service

$SchedService.Connect()

$Task = $SchedService.NewTask(0)

$Task.RegistrationInfo.Description = 'Balloon tip pop-up'

$Task.Settings.Enabled = $true

$Task.Settings.AllowDemandStart = $true

$trigger = $Task.triggers.Create(9)

$trigger.Enabled = $true

$action = $Task.Actions.Create(0)

$action.Path = 'C:\ProgramData\amagent\message.vbs'

$taskFolder = $SchedService.GetFolder('\')

$taskFolder.RegisterTaskDefinition('StartMsg', $Task , 6, 'Users', $null, 4)

Start-ScheduledTask -TaskName StartMsg



# Cleanup Scheduled Task and Script

Unregister-ScheduledTask -TaskName 'StartMsg' -Confirm:$false | Out-Null

Remove-Item -Path 'c:\ProgramData\Amagent\message.ps1' | Out-Null

Remove-Item -Path 'c:\ProgramData\Amagent\message.vbs' | Out-Null

4 replies

Forum|alt.badge.img
  • Power User
  • 52 replies
  • October 27, 2020

Are you facing issues with this?


jack.smith
Forum|alt.badge.img+1
  • Author
  • All Star
  • 168 replies
  • October 27, 2020

I’ve observed that it doesn’t always work when a user is connected over a terminal session. I think the issue may be in how the scheduled task is created to run as currently logged in user. Also have observed that on some systems it doesn’t execute at all, which may be related to execution policy. So I think there is room for improvement in those areas.


Forum|alt.badge.img
  • Power User
  • 52 replies
  • October 27, 2020

You might want to check if $currentusr is not null. Also, schedule task can be setup to run under the context of the local USERS group. That will work better.


jack.smith
Forum|alt.badge.img+1
  • Author
  • All Star
  • 168 replies
  • October 28, 2020

Updated code above to run as the Users group. Huge thanks to this code by @jesumyip for helping me see how to get there with a COM object. Create scheduled task to run at user log-on as user him/herself


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings