Import-Module Verbose输出表示模块已加载两次 [英] Import-Module Verbose output implies module is loaded twice

查看:102
本文介绍了Import-Module Verbose输出表示模块已加载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在非标准路径下(即不在$env:PSModulePath -split ";"中列出的通常位置下)有一个模块( MyModule ).但是,当我继续处理开发"副本时,我已将 MyModule 的生产"路径添加到该环境变量中.

I have a module (MyModule) under a non-standard path, i.e. not under the usual locations listed in $env:PSModulePath -split ";". I have, however, added the "production" path to MyModule to that environment variable while I continue to work on a "development" copy.

在尝试调试某些东西时,我使用以下命令加载了模块(使用$VerbosePreference = "Continue"),并立即看到详细输出的两行看似矛盾的行:

Whilst trying to debug something, I loaded the module (with $VerbosePreference = "Continue") using the following command and immediately saw two seemingly contradictory lines of Verbose output:

[D:\Dev\UserA\]> Import-Module D:\Dev\UserA\libs\PowerShell\MyModule

VERBOSE: Loading module from path 'D:\Dev\UserA\libs\PowerShell\MyModule\MyModule.psd1'.
VERBOSE: Loading module from path 'D:\Dev\usera\MyModule2\MyModule.psm1'.

我想了解为什么Import-Module似乎两次加载模块,尤其是第二条路径不正确时.

I would like to understand why Import-Module appears to be loading the module twice, especially as the second path is incorrect.

更多详细信息:

模块的文件夹结构为:

MyModule\MyModule.psd1
MyModule\MyModule.Test-Module.xml
MyModule\MyModule1\MyModule.psm1
MyModule\MyModule2\MyModule.psm1

注意(1)我在MyModule1子文件夹中保留了该模块的较旧版本1",并将更新后的版本2"文件放入MyModule2子文件夹中,并且(2)使用了.xml文件通过自定义模块测试脚本列出测试用例.我很确定后者可以忽略.

Note (1) I retained an older "version 1" of this module in a MyModule1 sub-folder and put my updated "version 2" file in a MyModule2 sub-folder and (2) that the .xml file is used by a custom module-testing script to list test cases. I'm pretty sure that the latter can be ignored.

我的模块清单(.psd1)文件包含以下内容,所有其他行均为空白或注释:

My module manifest (.psd1) file contains the following, with all other lines being blanks or comments:

@{
  RootModule = '.\MyModule2\MyModule.psm1'
  ModuleVersion = '2.0.0.0'
  GUID = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
  Author = 'Old Developer (v1.x) & New Developer (v2.x)'
  CompanyName = 'MyCompany'
  Copyright = '(c) 2013-2015 MyCompany. All rights reserved.'
  Description = 'Really useful functions'
  FileList = @(
    '.\MyModule.psd1'
    '.\MyModule.Test-Module.xml'
    '.\MyModule1\MyModule.psm1'
    '.\MyModule2\MyModule.psm1'
    '.\MyModule2\Examples\Archive-FilesWithCompression.ps1'
  )
}

很明显,我已经使用了文件的相对路径,尤其是. RootModule 键;这是必要的,因为我不确定共享模块时会将模块复制到何处.

Clearly I have used relative paths for the files, esp. the RootModule key; this is necessary as I cannot be sure where the module will be copied when I share it.

回到Verbose输出,我可以看到两行显示(1)PSD1文件的正确路径和(2)PSM1文件的无效路径.我确实注意到第二个路径的用户名小写,这就是我在测试前Set-Location时键入它的方式.因此,看起来第一个路径是通过将MyModule.psd1附加到给Import-Module cmdlet的路径上而获得的,第二个路径是(Get-Location)和RootModule路径的串联.

Going back to the Verbose output, I can see that the two lines show (1) the correct path to the PSD1 file and (2) an invalid path to the PSM1 file. I did notice that the second path has the username in lower-case, which is how I happened to type it when I Set-Location before testing. So, it looks like the first path is taken by appending MyModule.psd1 to the path given to the Import-Module cmdlet and the second is a concatenation of (Get-Location) and the RootModule path.

这似乎仅在此模块上发生.我在同一根"文件夹下还有其他文件夹,这些文件夹都没有这种行为.

This only seems to happen to this module. I have others under the same 'root' folder which don't exhibit this behaviour.

推荐答案

Um.好的.我可能已经解决了,至少有一部分...

Um. OK. I may have worked it out, at least in part...

我不小心在运行Import-Module cmdlet的位置下复制了 MyModule2 子文件夹.一旦我删除了该文件夹,详细的输出就会变得更加有意义:

I had accidentally made a copy of the MyModule2 sub-folder under the location from where I was running the Import-Module cmdlet. Once I removed that folder the verbose output started to make more sense:

[D:\Dev\UserA\]> Import-Module D:\Dev\UserA\libs\PowerShell\MyModule

VERBOSE: Loading module from path 'D:\Dev\UserA\libs\PowerShell\MyModule\MyModule.psd1'.
VERBOSE: Loading module from path 'D:\Dev\UserA\libs\PowerShell\MyModule\.\MyModule2\MyModule.psm1'.
VERBOSE: Exporting function '<function>'.
....

我猜这意味着,当PowerShell解析清单文件中的 RootModule 时,它将首先在当前路径下查找,然后在未找到任何内容的情况下在主模块文件夹下查找.我发现这与直觉相反,因为我希望清单中的任何相对路径始终相对于PSD1文件,而不是相对于当前位置.

I guess this means that when PowerShell resolves the RootModule in the manifest file it looks under the current path first and then the main module folder if nothing is found. I find this counter-intuitive as I'd expect that any relative paths in the manifest would always be relative to the PSD1 file, not the current location.

如果我随后尝试立即再次导入模块,我会得到:

If I then try to import the module again immediately I get this:

[D:\Dev\UserA\]> Import-Module D:\Dev\UserA\libs\PowerShell\MyModule

VERBOSE: Loading module from path 'D:\Dev\UserA\libs\PowerShell\MyModule\MyModule.psd1'.
VERBOSE: Importing function '<function>'.
....

也就是说,只有一行加载模块",而不是两行详细输出.然后,我尝试使用-Force开关并获得以下信息:

That is, only one "loading module" line, rather than the two lines of verbose output. I then tried the -Force switch and got the following:

[D:\Dev\UserA\]> Import-Module D:\Dev\UserA\libs\PowerShell\MyModule -Force

VERBOSE: Loading module from path 'D:\Dev\UserA\libs\PowerShell\MyModule\MyModule.psd1'.
VERBOSE: Removing the imported "<function>" function.
....
VERBOSE: Loading module from path 'D:\Dev\UserA\libs\PowerShell\MyModule\.\MyModule2\MyModule.psm1'.
VERBOSE: Exporting function '<function>'.
....
VERBOSE: Importing function '<function>'.
....

因此,似乎在第一次导入或强制重新导入模块时会出现两行冗长的输出,但如果它已经是会话的一部分,那么您只会看到第一行.

So, it seems that the two lines of verbose output appear when the module is first imported, or forcibly re-imported, but if it is already part of the session then you only see the first line.

此外,我最终发现,详细输出在 exporting 函数和 import 函数之间有所区别.这些功能按照在PSM1文件中定义的顺序 exported ,但是按字母顺序 importing .这建议使用一阶段或两阶段的过程,具体取决于是否从PSM1文件中重新读取功能定义,并结合我们看到的是一两行的详细输出.

Also, I finally spotted that the verbose output differentiated between exporting functions and importing them. The functions are exported in the order that they are defined in the PSM1 file, but are imported in alphabetical order. This suggests a one- or two-stage process is used depending on whether the function definitions are being re-read from the PSM1 file, tying in with whether we see one or two lines of verbose output.

或者,为了更直接地回答该问题,当需要读取.psm1文件时,似乎有两行显示正在加载模块...",而当PowerShell已经知道该模块时,只有一行显示为正在加载...".它的内容.

Or, to answer the question more directly, it appears that there are two lines saying "Loading module..." when it needs to read the .psm1 file, and only one line when PowerShell is already aware of the module and its contents.

这篇关于Import-Module Verbose输出表示模块已加载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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