简体   繁体   English

在模块/ Powershell中调用函数

[英]Calling a function within module / Powershell

I must be missing something basic here, but i'm new to powershell... 我必须遗漏一些基本的东西,但我是PowerShell的新手......

I wrote a function and saved it in a file called "UserSelectionList.psm1", the function is stubbed out like this: 我写了一个函数并将其保存在一个名为“UserSelectionList.psm1”的文件中,该函数被删除了如下:

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

i then try to call it with this script: 我然后尝试用这个脚本调用它:

Import-module "l:\support downstream\solarc\cngl\powershell scripts\userselectionlist.psm1"
$Options = "a","b","c"
cls
$result = UserSelectionList $Options
echo $result

The resulting error is: 产生的错误是:

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

I'm planning to have more than one function in a module, but this is where I'm at. 我打算在一个模块中有多个功能,但这就是我所处的位置。

thanks in advance 提前致谢

[Edit] I was not doing a Import Module with a -Force option. [编辑]我没有使用-Force选项进行导入模块。 The answer below is incorrect, but perhaps the Get-Command forced a refresh? 下面的答案是不正确的,但也许Get-Command强制刷新? Either way, I'm leaving it for completeness of the experience! 无论哪种方式,我都会离开它以获得完整的体验!

Thanks to latkin for pushing me to another path where i found this: 感谢latkin将我推到另一条道路,我发现了这个:

How do I retrieve command from a module 如何从模块中检索命令

Not only do you have to import a module, you then have to "get" it as well (?) 你不仅需要导入一个模块,还必须“获取”它(?)

Import-Module -Name <ModuleName> 
Get-Command -Module <ModuleName>

After I issued the Get-Command, everything started to work! 在我发出Get-Command后,一切都开始工作了!

Thanks latkin for quick response! 感谢latkin快速响应!

I've encountered the same problem. 我遇到了同样的问题。 Steps to reproduce: 重现步骤:

  • Write a PowerShell script with an Import-Module statement 使用Import-Module语句编写PowerShell脚本
  • Execute the script at a PowerShell prompt 在PowerShell提示符下执行脚本
  • Add a function to the imported module 向导入的模块添加功能
  • Modify the script to call the newly-added function 修改脚本以调用新添加的函数
  • Execute the script again, in the same PowerShell session 在同一PowerShell会话中再次执行脚本

The error went away after I added the -Force argument to Import-Module . -Force参数添加到Import-Module后,错误消失了。 The -Force argument can be removed once the function in the imported module is able to be called. 一旦能够调用导入模块中的函数,就可以删除-Force参数。

Note that latkin has alluded to this solution in his comment to the question. 请注意, 拉特金在对该问题的评论中提到了这个解决方案。 I'm hoping this will make it more visible. 我希望这会让它更加明显。

You only need to Get-Command if you didn't export the method properly from the module. 如果没有从模块中正确导出方法,则只需要Get-Command

At the end of your module put this: 在你的模块的最后把这个:

Export-ModuleMember -Function UserSelectionList

Note that it also accepts wild cards, so for example if you have 5 different Get-Some-Value functions that follow a naming convention you could just do 请注意,它也接受通配符,例如,如果你有5个不同的Get-Some-Value函数遵循命名约定,你可以做

Export-ModuleMember -Function Get-*

Side note on -Force : all that does is check if a module is already loaded and, if it is, removes it before continuing with the import. 关于-Force附注:所有这一切都是检查模块是否已经加载,如果是,则在继续导入之前将其删除。 It's the same as saying: 这跟说:

Remove-Module userselectionlist.psm1
Import-Module userselectionlist.psm1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM