Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PowerShell-如何在运行空间中导入模块_C#_Powershell_Powershell 2.0 - Fatal编程技术网

C# PowerShell-如何在运行空间中导入模块

C# PowerShell-如何在运行空间中导入模块,c#,powershell,powershell-2.0,C#,Powershell,Powershell 2.0,我正在尝试在C#中创建cmdlet。代码如下所示: [Cmdlet(VerbsCommon.Get, "HeapSummary")] public class Get_HeapSummary : Cmdlet { protected override void ProcessRecord() { RunspaceConfiguration config = RunspaceConfiguration.Create(); Runspace myRs


我正在尝试在C#中创建cmdlet。代码如下所示:

[Cmdlet(VerbsCommon.Get, "HeapSummary")]
public class Get_HeapSummary : Cmdlet
{
    protected override void ProcessRecord()
    {
        RunspaceConfiguration config = RunspaceConfiguration.Create();
        Runspace myRs = RunspaceFactory.CreateRunspace(config);
        myRs.Open();

        RunspaceInvoke scriptInvoker = new RunspaceInvoke(myRs);
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

        Pipeline pipeline = myRs.CreatePipeline();
        pipeline.Commands.Add(@"Import-Module G:\PowerShell\PowerDbg.psm1");
        //...
        pipeline.Invoke();

        Collection<PSObject> psObjects = pipeline.Invoke();
        foreach (var psObject in psObjects)
        {
            WriteObject(psObject);
        }
    }
}
[Cmdlet(VerbsCommon.Get,“HeapSummary”)]
公共类Get\u HeapSummary:Cmdlet
{
受保护的覆盖无效ProcessRecord()
{
RunspaceConfiguration config=RunspaceConfiguration.Create();
运行空间myRs=RunspaceFactory.CreateRunspace(配置);
myRs.Open();
RunspaceInvoke scriptInvoker=新的RunspaceInvoke(myRs);
调用(“设置执行策略不受限制”);
管道管道=myRs.CreatePipeline();
添加(@“导入模块G:\PowerShell\PowerDbg.psm1”);
//...
pipeline.Invoke();
集合psObjects=pipeline.Invoke();
foreach(psObjects中的var psObject)
{
WriteObject(psObject);
}
}
}
但尝试在PowerShell中执行此CmdLet时会出现以下错误:术语导入模块未被识别为CmdLet的名称。PowerShell中的同一命令未显示此错误。如果改为执行“Get Command”,则可以看到“Invoke Module”作为CmdLet列出

是否有办法在运行空间中执行“导入模块”


谢谢

以编程方式导入模块有两种方法,但我将首先介绍您的方法。您的行
pipeline.Commands.Add(“…”)
应该只添加命令,而不是命令和参数。单独添加该参数:

# argument is a positional parameter
pipeline.Commands.Add("Import-Module");
var command = pipeline.Commands[0];
command.Parameters.Add("Name", @"G:\PowerShell\PowerDbg.psm1")
上面的管道API使用起来有点笨拙,尽管它是许多更高级别API的基础,但在许多用途中被非正式地弃用。在powershell v2或更高版本中执行此操作的最佳方法是使用
System.Management.Automation.powershell
类型及其流畅的API:

# if Create() is invoked, a runspace is created for you
var ps = PowerShell.Create(myRS);
ps.Commands.AddCommand("Import-Module").AddArgument(@"g:\...\PowerDbg.psm1")
ps.Invoke()
使用后一种方法时的另一种方法是使用InitialSessionState预加载模块,这避免了使用
Import Module
显式地为运行空间种子

InitialSessionState initial = InitialSessionState.CreateDefault();
    initialSession.ImportPSModule(new[] { modulePathOrModuleName1, ... });
    Runspace runspace = RunspaceFactory.CreateRunspace(initial);
    runspace.Open();
    RunspaceInvoke invoker = new RunspaceInvoke(runspace);
    Collection<PSObject> results = invoker.Invoke("...");
InitialSessionState initial=InitialSessionState.CreateDefault();
initialSession.ImportPSModule(新[]{modulePathOrModuleName1,…});
Runspace Runspace=RunspaceFactory.CreateRunspace(初始值);
Open();
RunspaceInvoke invoker=新的RunspaceInvoke(runspace);
集合结果=invoker.Invoke(“…”);

希望这有帮助。

最简单的方法是使用
AddScript()
方法。 你可以做:

pipeline.AddScript("Import-Module moduleName").Invoke();
如果要在同一行中添加另一个导入

pipeline.AddScript("Import-Module moduleName \n Import-Module moduleName2").Invoke();
.Invoke()
不是必须的,只要将脚本添加到管道中,就可以添加更多脚本并在以后调用

pipeline.AddScript("Import-Module moduleName");
pipeline.AddCommand("pwd");
pipeline.Invoke();

有关更多信息,请访问

谢谢!但是Add()方法返回void。我想您需要使用一个命令对象,并向其添加一个参数,然后将其传递给add方法。您正在讨论以编程方式执行此操作的两种方法,第二种方法是什么?我还发现,如果在尝试导入模块时出现“AuthorizationManager检查失败”异常,请确保.psm1文件以UTF8格式保存,而不是ASCII格式。@x0n Oops。。链接死了(?)博客升级断了链接-叹气。