May 26, 2020 at 5:49 pm #231100
I’m creating my own module. Inside the module I have a function with the following code (shortened):
Enter-PSSession -ComputerName SERVER Import-Module MODULEX … Exit-PSSession
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.”
May 26, 2020 at 6:59 pm #231145ParticipantTopics: 3Replies: 431Points: 1,533Rank: Community Hero
Your function isn’t calling enter-pssession is it?
May 26, 2020 at 8:08 pm #231181
The function calls ‘Enter-PSSession’ at the begin and end the session afterwards. The code is all inside the same function.
May 26, 2020 at 9:05 pm #231199ParticipantTopics: 3Replies: 431Points: 1,533Rank: Community Hero
Enter-PSSession is an interactive command, not meant for scripts/functions.
May 27, 2020 at 1:15 am #231232ParticipantTopics: 0Replies: 30Points: 284Rank: Contributor
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.
Hope that helps.
May 27, 2020 at 6:56 pm #231391
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:
New-PSSessionConfigurationFile -Path $ConfigFile -ExecutionPolicy Restricted -LanguageMode NoLanguage -ModulesToImport MODULEX -SessionType RestrictedRemoteServer -VisibleCmdlets …., Import-Module, …. -VisibleExternalCommands ‘C:\Program Files\PROGRAM\util.exe’ -VisibleProviders FileSystem
If I manually open the endpoint and use Get-Command, I don’t get Import-Module as part of the response. Why?
May 28, 2020 at 3:05 am #231478ParticipantTopics: 0Replies: 30Points: 284Rank: Contributor
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.
- 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 (
Hope that helps.
May 28, 2020 at 7:19 pm #231712
I’ve used next command:
New-PSSessionConfigurationFile -Path $ConfigFile -ExecutionPolicy Unrestricted -LanguageMode NoLanguage -ModulesToImport MODULEX -SessionType RestrictedRemoteServer -VisibleCmdlets …,Import-Module,… -VisibleExternalCommands ‘C:\Program Files\PROGRAM\util.exe’ -VisibleProviders FileSystem
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:
New-PSSessionConfigurationFile -Path $ConfigFile -ExecutionPolicy Restricted -LanguageMode NoLanguage
For now, this problem is solved for me. But if you have any ideas to improve it, let me know.
- You must be logged in to reply to this topic.