Having to Import Modules

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
HRPlano
Posts: 3
Last visit: Fri Jul 28, 2023 12:08 pm

Having to Import Modules

Post by HRPlano »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2021, Version 5.8.192
Operating system: Windows 10 Pro, version 20H2 (OS Build 19042.1415)
PowerShell version(s): 5.1.19041.1320

*** Please add details and screenshots as needed below. ***
I am having to include Import-Module Modulename (e.g. Import-Module SQLServer or Import-Module ImportExcel) in my scripts. Sometimes, for some period of time, they seem to stay loaded and I don't have to, and then without having made any changes to OS, PowerShell Studio, or other edits to paths, modules, etc., when running the script, I will get:

ERROR: Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
ERROR: name, or if a path was included, verify that the path is correct and try again.

If I then uncomment Import-Module SQLServer, the script runs fine.

I have tried rebuilding the local cache and I have made sure that SQLServer and ImportExcel are checked if I go into Tools, Cache Editor.

Any suggestions for how to get the modules to "stick" would be appreciated.

Thanks,
Henson

DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Having to Import Modules

Post by Alexander Riedel »

The cache in PowerShell Studio and PrimalScript is module meta data, which allows us to provide intellisense for modules and their exported functions.
Loading cached meta data is faster than loading the actual modules and it allows us to provide intellisense for modules that are not and/or cannot be installed on your machine.

The cache configuration, included or excluded modules and their respective version have absolutely no influence on running a script.

In general, if you run a script in PowerShell Studio or in PrimalScript, a new runspace is created. If you run your scripts in a console, runspaces are preserved.
Since I do not know how you run your scripts, that is an important distinction to remember.

Most of the time, if you get module confusion on a machine, there are multiple versions installed. PowerShell will load the one it thinks is best, which unfortunately sometimes appears to be random.
I suggest as a first step to clean up any unused modules and versions on your machine.

If you get a "Invoke-Sqlcmd is not recognized..." error even though you have an explicit Import-Module statement, you should probably check the return of import-module.
Removing the import-module statement will make PowerShell auto-load the module, which likely loads another version. See above, best to clean that up.

PowerShell Module Manager (https://www.sapien.com/software/powersh ... ulemanager) might be able to help you with that.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.