Import module does not work

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 1 year and 8 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.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Import module does not work

Post by Eanna_bearn »

Product, version and build: 5.8.207_052722
Operating system: Windows 2k16 Server
PowerShell version(s): Powershell 5


Hello all from France,

I'm creating a tool that manage users in active directory, and many other automatic stuf. In this project i use NTFSSECURITY module in powershell.

On the first launch of the tool, it create all environment needed, registry keys, OU in AD etc ... it also copy files needed to import module with this function :

Code: Select all

function copiemodulentfs
{
	param (
		[Parameter(Mandatory = $true, Position = 0)]
		[string]$nomserveur)
	
	$ScriptDirectory = Get-ScriptDirectory
	$modulentfs = "Microsoft.PowerShell.Core\FileSystem::$($ScriptDirectory)\config\Module_powershell\ntfssecurity\"
	$destination = "Microsoft.PowerShell.Core\FileSystem::\\$nomserveur\c$\Program Files\WindowsPowerShell\Modules\"
	Copy-Item -path $modulentfs -Destination $destination -Recurse
	
}

$nomserveur is a variable for server name.

this function work well. After that i try to import module with this command :

Code: Select all

Import-module ntfssecurity
Nothing happens !

If i use this command in powershell directly module is imported. But i can't do it through Powershell Studio. Any ideas ?

Thanks !
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 1:00 pm
Answers: 39
Been upvoted: 30 times

Re: Import module does not work

Post by brittneyr »

What errors are you getting?
Can you confirm that the files you are copying are being correctly copied before importing when running in PowerShell Studio?
Brittney
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Import module does not work

Post by Eanna_bearn »

Thanks !

No error reported and yes files are copied correctly. It looks like the command import-module launched by powershell studio use/load the module when app is running but do not import it on computer ... is it possible ?
Commands of module NTFSSECURITY i use after that in my powershell studio script works, but when i launch powershell console with get-module -All module NTFSSECURITY is not listed.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 1:00 pm
Answers: 39
Been upvoted: 30 times

Re: Import module does not work

Post by brittneyr »

Is it listed if you run:
  1. Get-Module -ListAvailable
Something is being imported as your script would not be able to run the commands from the NTFSSECURITY module otherwise.
Brittney
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Import module does not work

Post by Eanna_bearn »

hello,

it is not listed but commands work ... i'll use the import-module command at every launch to be sure commands will work.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Import module does not work

Post by Alexander Riedel »

I cannot be sure but it appears that you assume that import-module needs to be called only once on a machine for any given module. Generally that is not true.
Import-Module imports the specified module into the current session. So if, as you described above, open a new PowerShell instance from your application you need to import that module again from that new session. There is a way to import a module automatically when a cmdlet of said module is used, but since cmdlet names are not always unique that is generally not such a good idea in my opinion.
Additionally, Get-Module -All does not list all your modules on your machine as the name might imply. It lists all dependent modules of the specified or loaded modules.
Get-Module -listavailable will list the modules available to load (import) on your machine PowerShell knows about. This list may be incomplete if you have modules that are installed outside of $psmodulepath.
If you use import-module and the cmdlets work, both without error, your module is loaded and operating correctly.
Alexander Riedel
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Import module does not work

Post by Eanna_bearn »

thanks for your answers ;)
My script work so i will continue
This topic is 1 year and 8 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.