Search code examples
powershellremote-serverpowershell-5.0

PowerShell Import-Module over remote connection


Problem: When I use a script to run another script remotely that relies on a module, the module is not imported.


I have 3 scripts that interact with each other. A Controller script, Body script, and Functions module.

The Controller is handling the authentication to the remote computer and executing the body script on the remote computer using the Invoke-Command.

The body is relying on the functions module.


If I run the `body` script directly on the remote computer, the module imports correctly and the script finishes; however, if I use the `controller` script to run the `body` script remotely, the module is never imported. If I nest the functions from the module in the body script (it looks messy) and run remotely, the script finishes, so I know it's an issue with importing the module. I've also tried importing the module into the `controller` script. It imports correctly, but when the `body` script executes it still can't find the functions in the module. (I'm guessing it imports into the local Powershell ... shell) I've tried running import module with the `-verbose` tag to see if it's importing, and it works in all instances except when `controller` is used to run `body` remotely.

Edit: I'm almost positive that it's because it's trying to find the module path on the remote computer and the path is referencing a location on the host computer. Aside from using an invoke-command from the remote computer back to the host to import the module, I'm not sure how to accomplish this.


Solution

  • Why not import the module using the network path in your code? If you know where the module is going to be, you can provide the path to it, even if it's on a remote machine