1

I need to be able to import a module that resides on a remote machine through a C# runspace.

To be clear what I mean by this: The service I'm creating resides on server A. It creates a remote runspace to server B using the method below. Through the runspace I'm trying to import a module on server B.

Here's the method I'm using to make the remote call:

internal Collection<PSObject> RunRemoteScript(string remoteScript, string remoteServer, string scriptName, out bool scriptSuccessful)
    {
        bool isLocal = (remoteServer == "localhost" || remoteServer == "127.0.0.1" || remoteServer == Environment.MachineName);

        WSManConnectionInfo connectionInfo = null;

        if (!isLocal)
        {
            connectionInfo = new WSManConnectionInfo(new Uri("http://" + remoteServer + ":5985"));
        }

        PsHostImplementation myHost = new PsHostImplementation(scriptName);

        using (Runspace remoteRunspace = (isLocal ? RunspaceFactory.CreateRunspace(myHost) : RunspaceFactory.CreateRunspace(myHost, connectionInfo)))
        {
            remoteRunspace.Open();
            using (PowerShell powershell = PowerShell.Create())
            {
                powershell.Runspace = remoteRunspace;

                Pipeline pipeline = remoteRunspace.CreatePipeline();

                pipeline.Commands.AddScript(remoteScript);

                Collection<PSObject> results = pipeline.Invoke();

                remoteRunspace.Close();

                scriptSuccessful = myHost.ScriptSuccessful;
                return results;
            }
        }
    }

"remoteScript" is set to the Powershell script I want to run. For example:

"Import-Module Modulename;CustomCommand-FromModule -parameter(s) -ErrorAction stop"

The module is not packedged together it's a psd1, psm1, and a bunch of script files that reside in C:\\Powershell\ModuleName on the remote server (server B) and I have tested and made sure that it is calling the ModuleName.psd1 file in the C:\\Powershell\ModuleName directory.

Inside the ModuleName.psd1 file is the line:

ModuleToProcess = 'ModuleName.psm1'

However I get a very weird error if I try to run it through the c# runspace.

If I send in the following as a parameter to "remoteScript":

"Import-Module Modulename"

I get the following error:

System.Management.Automation.RemoteException: The module to process 'ModuleName.psm1', listed in field 'ModuleToProcess' of module manifest 'C:\Powershell\ModuleName\ModuleName.psd1' was not processed because no valid module was found in any module directory.

The module does exist in one of the "$env:PSModulePath locations and does show up if you run:

get-module -listAvailable

I have also tried putting in the fully qualified path to the ModuleName.psm1 file inside the psd1 file. When I do that (or what was suggested by x0n below) I get almost the exact same error:

The module to process 'C:\Powershell\ModuleName\ModuleName.psm1', listed in field 'ModuleToProcess' of module manifest 'C:\Powershell\ModuleName\ModuleName.psd1' was not processed because no valid module was found in any module directory.

At this point I'm really not sure where to go or even if this is actually possible. I've searched everywhere and found some things that seem related, but were never quite the same and tended to be problems I've already overcome or just haven't (to my knowledge) come up against yet. Any help would be appreciated! Thanks!

8
  • 1
    Try using ModuleToProcess = Join-Path $psscriptroot 'modulename.psm1' in the PSD1. Commented Mar 23, 2012 at 14:09
  • Thanks x0n, but I still get an error. I added in a few more details to the original post. Commented Mar 27, 2012 at 15:43
  • 1
    Can you import the module through remoting on the command line? Commented Apr 19, 2012 at 3:32
  • any final solution about it with full source code ? What is PsHostImplementation ? Commented May 24, 2012 at 10:11
  • @AdamDriscoll I apologize, I honestly cannot remember if the import worked through a manual remote session or not. I think it worked, but I'm not certain anymore. If you need to know for a problem you're trying to solve, I can try to test it (I've since solved the problem, so I'd have to be careful in testing). Commented May 24, 2012 at 17:52

3 Answers 3

1

I suspect you need to do two things:

  1. Use the absolute path to the .psd1 file: otherwise only modules found in $env:PSModulePath will be considered (it matters not where the script doing the loading is running).

  2. Ensure you can load the remote script. The default or RemoteSigned execution policies will block remote scripts unless signed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the feedback. Both of the steps you mentioned are definitely things anyone would need to do (these are things I had made sure were done as part of my troubleshooting), but neither of these ended up being the final problem. I will be posting my final solution.
1

I found that if your module is built on a newer .net runtime(say .net 4.0) and you were trying to load it in powershell 2.0(based on .net 3.5) this can be an issue..to work around this, you need to make sure to load powershell or calling process in .net 4.0 clr.

You can do this by adding an enty for supported runtime in *.exe.config( Create powershell.exe.config if one does not exist already) as applicable.

I tested this works great for the above issue.

1 Comment

I've already solved the issue (I just hadn't gotten to posting the solution yet, my bad) but this sounds like something great to just try if the issue comes up again. Thank you!
1

It's been a little bit since I solved this so I apologize for the delay. The solution for this actually turned out to be rather easy, though a little weird.

To fix this, in ModuleName.psd1 I commented out:

ModuleToProcess = 'ModuleName.psm1'

And I Added it to NestedModules instead:

NestedModules = Join-Path $psscriptroot 'ModuleName.psm1'

Frankly, I'm not exactly sure why this let the module load successfully, but it worked for me.

It has been a little while since I solved this so it is possible there is some tiny step I missed here. If this doesn't work for you, let me know what problem you run in to and then I can let you know if it was something additional I had to do. I am fairly certain this was all though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.