1
votes

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.

1
have you tried to go into the ISE and debug it, if you do a step in and check each server if the functions are there. - Luke
@Luke I have. I figured out the issue is that the import-module command is being ran on the server and the path supplied is referencing a path on the host machine. So the script can't find the module at the supplied path on the server, because it's not there. At this point I'm trying to figure out the best way to include the module. - Speerian
without seeing your script I think that you should be able to start a PSsession on the remote machine and run it from there. - Luke
@Luke You're saying that I should start a PSSession on the remote machine to connect back to the host? Would that essentially be the same thing as using invoke-command to connect back to the host from the server? - Speerian
no the opposite connect the host to the remote using the session, it is similar but it will keep the session open allowing you to run many scripts through the one session. - Luke

1 Answers

0
votes

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