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.”
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.”
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).
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: