gpt4 book ai didi

powershell - 在模块/Powershell 中调用函数

转载 作者:行者123 更新时间:2023-12-04 01:45:24 24 4
gpt4 key购买 nike

我必须在这里遗漏一些基本的东西,但我是powershell的新手......

我编写了一个函数并将其保存在一个名为“UserSelectionList.psm1”的文件中,该函数被删除如下:

function Global:UserSelectionList([String[]] $UserOptions)
{
...
}

然后我尝试用这个脚本调用它:
Import-module "l:\support downstream\solarc\cngl\powershell scripts\userselectionlist.psm1"
$Options = "a","b","c"
cls
$result = UserSelectionList $Options
echo $result

产生的错误是:
The term 'UserSelectionList' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:5 char:28
+ $result = UserSelectionList <<<< $Options
+ CategoryInfo : ObjectNotFound: (UserSelectionList:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

我计划在一个模块中拥有多个功能,但这就是我所在的地方。

提前致谢

最佳答案

我遇到了同样的问题。重现步骤:

  • 使用 Import-Module 编写 PowerShell 脚本声明
  • 在 PowerShell 提示符处执行脚本
  • 为导入的模块添加函数
  • 修改脚本调用新增函数
  • 在同一个 PowerShell session 中再次执行脚本

  • 添加 -Force 后错误消失了 Import-Module 的参数. -Force一旦能够调用导入模块中的函数,就可以删除参数。

    请注意 latkin在他的 comment 中提到了这个解决方案问题。我希望这会让它更加明显。

    关于powershell - 在模块/Powershell 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591281/

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