Reputation: 2018
I must be missing something basic here, but i'm new to powershell...
I wrote a function and saved it in a file called "UserSelectionList.psm1", the function is stubbed out like this:
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
Upvotes: 4
Views: 29130
Reputation: 559
I've encountered the same problem. Steps to reproduce:
Import-Module
statementThe error went away after I added the -Force
argument to Import-Module
. The -Force
argument can be removed once the function in the imported module is able to be called.
Note that latkin has alluded to this solution in his comment to the question. I'm hoping this will make it more visible.
Upvotes: 8
Reputation: 8098
You only need to Get-Command
if you didn't export the method properly from the module.
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
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. It's the same as saying:
Remove-Module userselectionlist.psm1
Import-Module userselectionlist.psm1
Upvotes: 3
Reputation: 2018
[Edit] I was not doing a Import Module with a -Force option. The answer below is incorrect, but perhaps the Get-Command forced a refresh? Either way, I'm leaving it for completeness of the experience!
Thanks to latkin for pushing me to another path where i found this:
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!
Thanks latkin for quick response!
Upvotes: 4