I am trying to build a messaging system that is launched by scheduled task, and will eventually allow me to send messages to users before triggering software update jobs.
I have the UserMessage script basically working...
class PxMessage {
static [PxMessage] $instance
static [windows.forms.notifyIcon] $balloon
static [drawing.icon] $defaultIcon
static [PxMessage] GetInstance($processID) {
if ([PxMessage]::instance -eq $null) {
[PxMessage]::instance = [PxMessage]::new()
#[void][reflection.assembly]::LoadWithPartialName('Windows.Forms')
[PxMessage]::balloon = [windows.forms.notifyIcon]::New()
[PxMessage]::defaultIcon = [drawing.icon]::ExtractAssociatedIcon($(Get-Process -id:$processID | Select-Object -expandProperty:path))
}
return [PxMessage]::instance
}
[Void] SendMessage ([String]$title, [String]$message, [String]$messageIcon, [String]$icon) {
if ($icon) {
[PxMessage]::balloon.icon = [drawing.icon]::ExtractAssociatedIcon($icon) # must be a local path
} else {
[PxMessage]::balloon.icon = [PxMessage]::defaultIcon
}
[PxMessage]::balloon.balloonTipTitle = $title
[PxMessage]::balloon.balloonTipText = $message
[PxMessage]::balloon.balloonTipIcon = $messageIcon
[PxMessage]::balloon.visible = $true
[PxMessage]::balloon.ShowBalloonTip(0)
[void][PxMessage]::balloon.Dispose
}
}
$message = [PxMessage]::GetInstance($pid)
$message.SendMessage('Title', 'Message', 'Info', 'C:\PxIcon.ico')
This works when run in the ISE, and it works when I run it in the console from another script using & "\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\ScheduledTasks\UserMessage.ps1"
.
However, when I set up a scheduled task like this...
$runTime = (get-Date) + (New-TimeSpan -seconds:1)
$action = New-ScheduledTaskAction -execute:'powershell.exe' -argument:'-NonInteractive -NoLogo -NoProfile -noExit -executionPolicy Bypass -File "\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\ScheduledTasks\UserMessage_class.ps1"'
$trigger = New-ScheduledTaskTrigger -once -at:$runTime
$settings = New-ScheduledTaskSettingsSet
$task = New-ScheduledTask -action:$action -trigger:$trigger -settings:$settings
Register-ScheduledTask -taskName:'Px Tools' -inputObject:$task
I get the error Unable to find type [Windows.Forms.NotifyIcon].
Is there some interaction with Scheduled Tasks that causes problems with loading assemblies?
I know that [reflection.assembly]::LoadWithPartialName
has been deprecated, and I found this which outlines some options. I tried
[reflection.assembly]::LoadFrom("C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll")
, with the same results.