簡體   English   中英

加載程序集並按計划任務運行不兼容?

[英]Loading assemblies & running as scheduled task not compatible?

我正在嘗試構建一個由計划任務啟動的消息傳遞系統,最終將允許我在觸發軟件更新作業之前向用戶發送消息。

我的 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

我收到錯誤Unable to find type [Windows.Forms.NotifyIcon]. 與計划任務的交互是否會導致加載程序集出現問題?

我知道[reflection.assembly]::LoadWithPartialName已被棄用,我發現概述了一些選項。 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同樣的結果。

您是否像這樣加載程序集

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

或如此處所示

Add-Type -AssemblyName System.Windows.Forms

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM