C# 如何从c捕获PowerShell函数调用#
我需要从已创建的PowerShell主机捕获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
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。