title: Import a Powershell Module on a Remote Server in a C# Runspace
body: <p>I need to be able to import a module that resides on a remote machine through a C# runspace.</p>
  
  <p>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.</p>
  
  <p>Here's the method I'm using to make the remote call:</p>
  
  <pre><code>internal Collection&lt;PSObject&gt; 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&lt;PSObject&gt; results = pipeline.Invoke();
  
                  remoteRunspace.Close();
  
                  scriptSuccessful = myHost.ScriptSuccessful;
                  return results;
              }
          }
      }
  </code></pre>
  
  <p>"remoteScript" is set to the Powershell script I want to run.  For example:</p>
  
  <pre><code>"Import-Module Modulename;CustomCommand-FromModule -parameter(s) -ErrorAction stop"
  </code></pre>
  
  <p>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.  </p>
  
  <p>Inside the ModuleName.psd1 file is the line:</p>
  
  <pre><code>ModuleToProcess = 'ModuleName.psm1'
  </code></pre>
  
  <p>However I get a very weird error if I try to run it through the c# runspace.  </p>
  
  <p>If I send in the following as a parameter to "remoteScript":</p>
  
  <pre><code>"Import-Module Modulename"
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>The module does exist in one of the "$env:PSModulePath locations and does show up if you run:</p>
  
  <pre><code>get-module -listAvailable
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>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!</p>