Import-Module inside my own module

Welcome Forums General PowerShell Q&A Import-Module inside my own module

Viewing 7 reply threads
  • Author
    Posts
    • #231100
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

      Hi

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

      What’s wrong?

      Greetings

    • #231145
      Participant
      Topics: 3
      Replies: 431
      Points: 1,533
      Helping Hand
      Rank: Community Hero

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

    • #231181
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

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

    • #231199
      Participant
      Topics: 3
      Replies: 431
      Points: 1,533
      Helping Hand
      Rank: Community Hero

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

    • #231232
      Participant
      Topics: 0
      Replies: 30
      Points: 284
      Helping Hand
      Rank: Contributor

      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.

    • #231391
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

      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?

    • #231478
      Participant
      Topics: 0
      Replies: 30
      Points: 284
      Helping Hand
      Rank: 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.

      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.

    • #231712
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

      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.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.