Powershell 装载组件和;按计划任务运行不兼容?

Powershell 装载组件和;按计划任务运行不兼容?,powershell,scheduled-tasks,Powershell,Scheduled Tasks,我正在尝试构建一个由预定任务启动的消息传递系统,并最终允许我在触发软件更新作业之前向用户发送消息 我的UserMessage脚本基本正常工作 class PxMessage { static [PxMessage] $instance static [windows.forms.notifyIcon] $balloon static [drawing.icon] $defaultIcon static [PxMessage] GetInstance($proc

我正在尝试构建一个由预定任务启动的消息传递系统,并最终允许我在触发软件更新作业之前向用户发送消息

我的UserMessage脚本基本正常工作

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')
这在ISE中运行时有效,在控制台中使用
&“\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\ScheduledTasks\UserMessage.ps1”
从另一个脚本运行时有效

但是,当我设置这样的计划任务时

$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
我发现错误
找不到类型[Windows.Forms.NotifyIcon]。
与计划任务的交互是否会导致加载程序集的问题

我知道,
[reflection.assembly]::LoadWithPartialName
已被弃用,我发现其中列出了一些选项。我试过了
[reflection.assembly]::LoadFrom(“C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0_b77a5c561934e089/System.Windows.Forms.dll”)
,具有相同的结果。

您喜欢这样吗:

[void][System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
或作为:

添加类型-AssemblyName System.Windows.Forms

我两种方法都试过了。第一个抛出错误,第二个没有错误,但代码也不工作。我确实想知道,如果我在静态属性定义中使用类型,但在加载所需的程序集之前,该如何工作?您也可以使用assembly system.drawing@js2010执行
,您将如何执行该操作。在静态声明之前和之后的类中,以及在GetInstance方法中,我完全按照您的方式尝试了它,而不是在方法之前和开始时。所有标记均为错误(红色下划线)。即使将它们放在脚本文件的顶部也不起作用(没有错误,但也没有气球提示),这不会导致类自包含,我认为这是可行的;使用命名空间system.windows.forms;[消息框]::显示('hi')