我必须遗漏一些基本的东西,但我是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
我打算在一个模块中有多个功能,但这就是我所处的位置。
提前致谢
答案 0 :(得分:2)
[编辑]我没有使用-Force选项进行导入模块。下面的答案是不正确的,但也许Get-Command强制刷新?无论哪种方式,我都会离开它以获得完整的体验!
感谢拉特金把我推到另一条道路,我发现了这个:
How do I retrieve command from a module
您不仅需要导入模块,还必须“获取”模块(?)
Import-Module -Name <ModuleName>
Get-Command -Module <ModuleName>
发出Get-Command后,一切都开始工作了!
感谢latkin快速回复!
答案 1 :(得分:1)
如果您没有从模块中正确导出方法,则只需Get-Command
。
在你的模块结束时把它:
Export-ModuleMember -Function UserSelectionList
请注意,它也接受通配符,例如,如果您有5个不同的Get-Some-Value函数,它们遵循命名约定,您可以这样做
Export-ModuleMember -Function Get-*
关于-Force
的旁注:所有工作都是检查模块是否已经加载,如果是,则在继续导入之前将其删除。它与说法相同:
Remove-Module userselectionlist.psm1
Import-Module userselectionlist.psm1
答案 2 :(得分:1)