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!