在C#运行空间中的远程服务器上导入Powershell模块 [英] Import a Powershell Module on a Remote Server in a C# Runspace

查看:162
本文介绍了在C#运行空间中的远程服务器上导入Powershell模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够通过C#运行空间导入驻留在远程计算机上的模块.

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

要清楚我的意思是:我正在创建的服务驻留在服务器A上.它使用以下方法为服务器B创建一个远程运行空间.通过运行空间,我试图在服务器B上导入模块.

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"设置为我要运行的Powershell脚本.例如:

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

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

该模块未打包在一起,它是一个psd1,psm1和一堆脚本文件,它们驻留在远程服务器(服务器B)上的C:\\ Powershell \ ModuleName中,我已经测试并确保已在调用C:\\ Powershell \ ModuleName目录中的ModuleName.psd1文件.

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.

在ModuleName.psd1文件内部是以下行:

Inside the ModuleName.psd1 file is the line:

ModuleToProcess = 'ModuleName.psm1'

但是,如果尝试通过c#运行空间运行它,则会收到一个非常奇怪的错误.

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

如果我将以下内容作为参数发送到"remoteScript":

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

"Import-Module Modulename"

我收到以下错误:

System.Management.Automation.RemoteException:未处理模块清单清单C:\ Powershell \ ModuleName \ ModuleName.psd1'的字段'ModuleToProcess'中列出的用于处理'ModuleName.psm1'的模块.可以在任何模块目录中找到.

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.

该模块确实存在于"$ env:PSModulePath位置之一"中,并且在运行时会显示:

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

get-module -listAvailable

我还尝试将psd1文件内的ModuleName.psm1文件的完全限定路径放入.当我这样做时(或下面x0n的建议),我几乎得到了完全相同的错误:

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:

未处理模块清单清单"C:\ Powershell \ ModuleName \ ModuleName.psd1"的字段"ModuleToProcess"中列出的用于处理"C:\ Powershell \ ModuleName \ ModuleName.psd1"的模块,因为未找到有效的模块在任何模块目录中.

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!

推荐答案

自从我解决了这个问题以来,已经有点过头了,因此我为您的延迟深表歉意.事实证明,解决方案相当简单,尽管有点怪异.

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.

要解决此问题,我在ModuleName.psd1中将其注释掉:

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

ModuleToProcess = 'ModuleName.psm1'

然后我将其添加到NestedModules中:

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.

这篇关于在C#运行空间中的远程服务器上导入Powershell模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆