Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# 如何从c捕获PowerShell函数调用#_C#_.net_Function_Powershell_Overriding - Fatal编程技术网

C# 如何从c捕获PowerShell函数调用#

C# 如何从c捕获PowerShell函数调用#,c#,.net,function,powershell,overriding,C#,.net,Function,Powershell,Overriding,我需要从已创建的PowerShell主机捕获PowerShell函数调用并覆盖它。这怎么可能 InitialSessionState iss = InitialSessionState.CreateDefault(); iss.LanguageMode = PSLanguageMode.FullLanguage; iss.ImportPSModule(new string[] { "TestModule.psm1" }); Runspace rs = RunspaceFactory.Create

我需要从已创建的PowerShell主机捕获PowerShell函数调用并覆盖它。这怎么可能

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.LanguageMode = PSLanguageMode.FullLanguage;
iss.ImportPSModule(new string[] { "TestModule.psm1" });
Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();
Pipeline pl = rs.CreatePipeline();
pl.Commands.AddScript("TestFunction testParam");
var res = pl.Invoke();
rs.Close();
我的任务是动态覆盖TestFunction,它位于TestModule中,我该怎么做

您可以定义一个名为“TestFunction”的cmdlet(在c#代码中),并将其添加到运行空间配置中,如下所示:

 rs.RunspaceConfiguration.Cmdlets.Append(New CmdletConfigurationEntry("testfunction", GetType(TestFuncitonCmdlet), Nothing))

谢谢!但我可以在打开运行空间后更改运行空间配置吗?当然可以。至少我知道您可以在其上更改cmdlet集合。我在我编写的主机中这样做是为了提供以特殊方式与主机交互的cmdlet。