Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function within module / Powershell

Tags:

powershell

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

like image 611
codeputer Avatar asked Oct 31 '25 20:10

codeputer


1 Answers

I've encountered the same problem. Steps to reproduce:

  • Write a PowerShell script with an Import-Module statement
  • Execute the script at a PowerShell prompt
  • 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

The 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.

like image 53
Steve Lang Avatar answered Nov 04 '25 02:11

Steve Lang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!