title: Loading assemblies & running as scheduled task not compatible?
body: <p>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.</p>
  
  <p>I have the UserMessage script basically working...</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>This works when run in the ISE, and it works when I run it in the console from another script using <code>&amp; "\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\ScheduledTasks\UserMessage.ps1"</code>.</p>
  
  <p>However, when I set up a scheduled task like this...</p>
  
  <pre><code>$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
  </code></pre>
  
  <p>I get the error <code>Unable to find type [Windows.Forms.NotifyIcon].</code> Is there some interaction with Scheduled Tasks that causes problems with loading assemblies?</p>
  
  <p>I know that <code>[reflection.assembly]::LoadWithPartialName</code> has been deprecated, and I found <a href="https://www.madwithpowershell.com/2013/10/add-type-vs-reflectionassembly-in.html" rel="nofollow noreferrer">this</a> which outlines some options. I tried
  <code>[reflection.assembly]::LoadFrom("C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll")</code>, with the same results.</p>