首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载程序集与按计划任务运行不兼容?

加载程序集与按计划任务运行不兼容?
EN

Stack Overflow用户
提问于 2019-10-29 17:35:17
回答 1查看 120关注 0票数 0

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

我已经让UserMessage脚本基本正常工作了……

代码语言:javascript
复制
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"从另一个脚本在控制台中运行它时也有效。

但是,当我像这样设置一个计划任务时...

代码语言:javascript
复制
$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

我收到错误消息Unable to find type [Windows.Forms.NotifyIcon].是否与计划任务发生了一些交互,从而导致加载程序集出现问题?

我知道[reflection.assembly]::LoadWithPartialName已经被弃用了,我找到了this,它概述了一些选项。我尝试了[reflection.assembly]::LoadFrom("C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll"),结果是一样的。

EN

回答 1

Stack Overflow用户

发布于 2019-10-29 22:33:06

你是这样load the assemblies的吗:

代码语言:javascript
复制
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

或作为indicated here

代码语言:javascript
复制
Add-Type -AssemblyName System.Windows.Forms
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58604670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档