Import-Module inside my own module

Hi

I’m creating my own module. Inside the module I have a function with the following code (shortened):

[pre]Enter-PSSession -ComputerName SERVER
Import-Module MODULEX

Exit-PSSession[/pre]

The function imports a module which is installed on the remote server. If I enter the code on my own, it works. If I call the function, the error says “The specified module ‘MODULEX’ was not loaded because no valid module file was found in any module directory.”

What’s wrong?

Greetings

Your function isn’t calling enter-pssession is it?

The function calls ‘Enter-PSSession’ at the begin and end the session afterwards. The code is all inside the same function.

Enter-PSSession is an interactive command, not meant for scripts/functions.

Hello win54,

Enter-PSSession - Starts an interactive session with a remote computer.

If you need to connect to remote computer use New-PSSession to establish session and then use Invoke-Command to run cmdlets within the session.

References:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssession?view=powershell-7

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7

 

Hope that helps.

Thanks, Andy and Doug. With New-PSSession and Invoke-Command it works for the default admin endpoint. With the constrained endpoint, it fails with the error: “The term ‘Import-Module’ is not recognized as the name of a cmdlet, function, script file, or operable program.”

My pssc-file was created with next command:

[pre]New-PSSessionConfigurationFile -Path $ConfigFile -ExecutionPolicy Restricted -LanguageMode NoLanguage -ModulesToImport MODULEX -SessionType RestrictedRemoteServer -VisibleCmdlets …, Import-Module, … -VisibleExternalCommands ‘C:\Program Files\PROGRAM\util.exe’ -VisibleProviders FileSystem[/pre]

If I manually open the endpoint and use Get-Command, I don’t get Import-Module as part of the response. Why?

It might be because of ExecutionPolicy:

PowerShell’s execution policy is a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts.

Restricted

  • The default execution policy for Windows client computers.
  • Permits individual commands, but does not allow scripts.
  • Prevents running of all script files, including formatting and configuration files (.ps1xml), module script files (.psm1), and PowerShell profiles (.ps1).
 

Reference: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7

Hope that helps.

I’ve used next command:

[pre]New-PSSessionConfigurationFile -Path $ConfigFile -ExecutionPolicy Unrestricted -LanguageMode NoLanguage -ModulesToImport MODULEX -SessionType RestrictedRemoteServer -VisibleCmdlets …,Import-Module,… -VisibleExternalCommands ‘C:\Program Files\PROGRAM\util.exe’ -VisibleProviders FileSystem[/pre]

Even with FullLanguage and SessionType Default it gives errors. After some testing, the next command creates a pssc-file which lets me import the module:

[pre]New-PSSessionConfigurationFile -Path $ConfigFile -ExecutionPolicy Restricted -LanguageMode NoLanguage[/pre]

For now, this problem is solved for me. But if you have any ideas to improve it, let me know.