gpt4 book ai didi

powershell - #Requires 看不到模块,Import-Module 有效

转载 作者:行者123 更新时间:2023-12-03 01:14:55 25 4
gpt4 key购买 nike

当我用 #Requires -Version 4 -Modules TestModule 运行一个简单的脚本时在第一行声明,我得到一个错误:

The script 'TestScript.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: TestModule CategoryInfo : ResourceUnavailable: (TestScript.ps1:String) [], ScriptRequiresException FullyQualifiedErrorId : ScriptRequiresMissingModules



一些有趣的事实:
  • 我第二次运行这个脚本(同一个 session ),模块被加载并且一切正常。
  • 脚本目录位于网络共享上,但其他 7 个模块加载时没有任何问题(模块类型是 list 、脚本和二进制 - 它们都可以工作)。
  • 其中一个模块还有 #Requires -Version 4 -Modules TestModule声明,但是当我使用 #Requires 运行脚本时指向该模块,两个模块都加载没有任何问题。
  • 路径明显添加到PSModulePath .
  • 在运行脚本之前,Get-Module -ListAvailable确实列出了 TestModule 及其所有命令。
  • Import-Module第一次工作。
  • 机器上可能缺少某些东西:登录该计算机的任何人都遇到了同样的问题。
  • $PSVersionTable :
    Name                           Value
    ---- -----
    PSVersion 4.0
    WSManStackVersion 3.0
    SerializationVersion 1.1.0.1
    CLRVersion 4.0.30319.18444
    BuildVersion 6.3.9600.16406

    我不知道是怎么回事。

    最佳答案

    找到了原因。
    我替换了#RequiresImport-Module TestModule -Verbose并发现其中一个程序集无法加载,产生错误,但由于 $ErrorActionPreference=Continue模块的其余部分已加载。看来 #Requires行为不同于 Import-Module .

    关于powershell - #Requires 看不到模块,Import-Module 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31945984/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com