预加载模块文件所需的程序集:清单不起作用 [英] Preloading assemblies needed by a module file: manifest not working

查看:70
本文介绍了预加载模块文件所需的程序集:清单不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给予

UserMessage_class_script.ps1

Using module ".\UserMessage_class_script.psm1"
$processIcon = [System.Drawing.Icon]::ExtractAssociatedIcon($(Get-Process -id:$PID | Select-Object -expandProperty:path))
$message = [PxMessage]::GetInstance($processIcon)
$message.SendMessage('Title', "$(Get-Date)", 'Info')

UserMessage_class_script.psm1

class PxMessage {
    static [PxMessage] $instance
    static [Windows.Forms.NotifyIcon]$balloon
    static [System.Drawing.icon]$defaultIcon

    static [PxMessage] GetInstance($processIcon) {
        if ([PxMessage]::instance -eq $null) {
            [PxMessage]::instance = [PxMessage]::new()
            [PxMessage]::balloon = [Windows.Forms.NotifyIcon]::new()
            [PxMessage]::defaultIcon = $processIcon
        }

        return [PxMessage]::instance
    }

    [Void] SendMessage ([String]$title, [String]$message, [String]$messageIcon) {
        [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)
        [PxMessage]::balloon.Dispose
    }
}

UserMessage_class_script.psd1

@{
    RootModule = 'UserMessage_class_script.psm1'
    GUID = '0e53745b-0b05-4bd3-8af2-f1595bca0167'
    RequiredAssemblies = @('System.Drawing', 'System.Windows.Forms')
}

然后从快捷方式启动UserMessage_class_script.ps1,而不是在ISE中加载并运行它,我的理解是PSD1文件应该预加载所需的程序集,以便在使用模块在PS1文件中,PSM1文件中类的类型化属性不会引发错误.但是,那不是我所看到的.这两个属性行均引发无法找到类型错误.

And launching UserMessage_class_script.ps1 from a shortcut, not loading and running it in the ISE, my understanding is that the PSD1 file should preload the required assemblies so that when the PSM1 file is loaded by Using module in the PS1 file the typed properties of the class in the PSM1 file will not throw errors. However, that is not what I am seeing. Both Properties lines throw an Unable to find type error.

现在,如果我添加

using assembly System.Drawing
using assembly System.Windows.Forms

在PS1文件中的使用模块"行之前,所有步骤均有效.哪种方式有意义,当装入PSM1文件并编译类时,就可以使用程序集,而类型是可用的.此处.

before the Using module line in the PS1 file, it all works. Which kind of makes sense, the assemblies are present when the PSM1 file is loaded and the class is compiled, the type is available. This was explained well here.

但是这种方法使PSM1文件依赖于准备PS1文件中的环境,我认为我不需要这样做.所以我认为我在使用清单文件时做错了什么,尽管我一生都无法弄清楚是什么.

But this approach makes the PSM1 file dependent on preparing the environment in the PS1 file, which I THINK I shouldn't need to do. So I assume I am doing something wrong in my use of the manifest file, though for the life of me I can't figure out what.

如果必须的话,主脚本中的两条Using汇编行确实不是问题.但是,如果可以使PSM1(或带有清单的PSM1)自立,那会更好.

If I have to, the two Using assembly lines in the main script are really not an issue. But if the PSM1 (or the PSM1 in conjunction with a manifest) can be made self standing, that would be better.

推荐答案

我尝试重现此问题,发现2个问题.如果你解决了这个问题,它应该会按预期工作.

I tried to reproduce this and found 2 issues. If you fix that, it should work as intended.

首先,您的psd1可能永远不会加载.使用模块需要模块名称(请参阅

First, your psd1 is probably never loaded. Using module requires the module name (see the docs), not the psm1 path. If you haven't done so yet, put your module in a folder in one of the valid module locations from $Env:PSModulePath. The folder structure should look like this:

<module folder>
└───UserMessage_class_script
    └───UserMessage_class_script.psd1
    ├───UserMessage_class_script.psm1

然后将 UserMessage_class_script.ps1 中的行更改为使用模块名称:

And then change the line in UserMessage_class_script.ps1 to use the module name:

using module UserMessage_class_script

其次,Powershell抱怨需要 ModuleVersion ,因此请将其添加到您的mainfest中:

Secondly, Powershell complained that ModuleVersion is required, so add that to your mainfest:

@{
    RootModule = 'UserMessage_class_script.psm1'
    ModuleVersion = "1.0.0.0"
    GUID = '0e53745b-0b05-4bd3-8af2-f1595bca0167'
    RequiredAssemblies = @('System.Drawing', 'System.Windows.Forms')
}

我遵循了以下步骤,并使其能够工作.让我知道是否有帮助或有任何疑问.

I followed these steps and was able to make it work. Let me know if it helps or if you have questions.

这篇关于预加载模块文件所需的程序集:清单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆