我创建了一个.NET库,我想创建一个通过PowerShell调用库的GUI。为了调用cmdlet,我必须将an entry添加到Windows注册表中,因为该库是针对.NET 4编译的。
它工作正常,但不适用于GUI中的PowerShell主机。如何设置PowerShell主机以运行CLR 4管理单元?
var rsConfig = RunspaceConfiguration.Create();
var myAssembly = new AssemblyConfigurationEntry("AssemblyName", "C:\...\Assembly.dll");
rsConfig.Assemblies.Append(myAssembly);
var runSpace = RunspaceFactory.CreateRunspace(rsConfig);
runSpace.Open();
using (var ps = PowerShell.Create()) {
ps.Runspace = runSpace;
ps.AddCommand("Get-MyCmdlet");
ps.AddParameter("Param1");
ps.AddParameter("Param2");
foreach (var result in ps.Invoke()) { // CommandNotFoundException
Debug.WriteLine(result.ToString());
}
}