I have an engine that executes powershell scripts, and when they execute, need to feed back in some xml to the caller. The engine only takes i/o as an idmef xml message. So the script needs to return a similarly formatted xml message. I have a class which does my formatting for me and it would like the script writers to use it.
我有一个执行powershell脚本的引擎,当它们执行时,需要将一些xml反馈给调用者。该引擎仅将i / o作为idmef xml消息。因此脚本需要返回类似格式的xml消息。我有一个类为我做格式化,它希望脚本编写者使用它。
So question is I want to wrap a c# class to enable it to be used by powershell.
所以问题是我想包装一个c#类以使它能够被powershell使用。
I saw something some where you can refer to c# class using the square bracket mob, eg.
我看到了一些你可以使用方括号mob来引用c#类的东西,例如。
How to you make the c# available to be used like this. I reckon it needs to be made into class lib and somehow loaded into the powershell runtime but how. Also as I'm running the powershell from c#, how do I add it into the environment at that point.
如何使c#可以像这样使用。我认为它需要被制作成类lib并以某种方式加载到powershell运行时但是如何。另外,当我从c#运行powershell时,如何在此时将其添加到环境中。
Any help would be appreciated. Bob.
任何帮助,将不胜感激。鲍勃。
4 个解决方案
#1
If have an assembly (exe or dll) with the class in it, PowerShell can load it via [System.Reflection.Assembly]::LoadFile("PathToYourAssembly")
or in V2
如果有一个包含类的程序集(exe或dll),PowerShell可以通过[System.Reflection.Assembly] :: LoadFile(“PathToYourAssembly”)或V2加载它
Add-Type -Path "PathToYourAsembly"
If you are creating a runspace in your application and would like to make an assembly available, you can do that with a RunspaceConfiguration.
如果要在应用程序中创建运行空间并希望使程序集可用,则可以使用RunspaceConfiguration执行此操作。
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
AssemblyConfigurationEntry myAssembly = new AssemblyConfigurationEntry("strong name for my assembly", "optional path to my assembly");
rsConfig.Assemblies.Append(myAssembly);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
#2
I don't think anything is necessary. You can access pretty much the entire .NET Framework from PowerShell. I'm sure they didn't create any wrappers to do it.
我认为没有必要。您可以从PowerShell访问几乎整个.NET Framework。我敢肯定他们没有创造任何包装来做到这一点。
#3
Dealing with interfaces in PowerShell is very difficult if not impossible at least in V1, so avoid these in your class. In PowerShell a simple [reflection.assembly]::Load or LoadFile is all it takes.
至少在V1中处理PowerShell中的接口是非常困难的,所以在课堂上要避免这些。在PowerShell中,只需要一个简单的[reflection.assembly] :: Load或LoadFile。
#4
I would write a Cmdlet in C# that takes a bunch of parameters and spits out the XML that you want. Then you can use that Cmdlet from Powershell by piping things into it. I hope this helps!
我会在C#中编写一个Cmdlet,它接受一堆参数并吐出你想要的XML。然后你就可以使用Powershell中的Cmdlet来管理它。我希望这有帮助!
#1
If have an assembly (exe or dll) with the class in it, PowerShell can load it via [System.Reflection.Assembly]::LoadFile("PathToYourAssembly")
or in V2
如果有一个包含类的程序集(exe或dll),PowerShell可以通过[System.Reflection.Assembly] :: LoadFile(“PathToYourAssembly”)或V2加载它
Add-Type -Path "PathToYourAsembly"
If you are creating a runspace in your application and would like to make an assembly available, you can do that with a RunspaceConfiguration.
如果要在应用程序中创建运行空间并希望使程序集可用,则可以使用RunspaceConfiguration执行此操作。
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
AssemblyConfigurationEntry myAssembly = new AssemblyConfigurationEntry("strong name for my assembly", "optional path to my assembly");
rsConfig.Assemblies.Append(myAssembly);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
#2
I don't think anything is necessary. You can access pretty much the entire .NET Framework from PowerShell. I'm sure they didn't create any wrappers to do it.
我认为没有必要。您可以从PowerShell访问几乎整个.NET Framework。我敢肯定他们没有创造任何包装来做到这一点。
#3
Dealing with interfaces in PowerShell is very difficult if not impossible at least in V1, so avoid these in your class. In PowerShell a simple [reflection.assembly]::Load or LoadFile is all it takes.
至少在V1中处理PowerShell中的接口是非常困难的,所以在课堂上要避免这些。在PowerShell中,只需要一个简单的[reflection.assembly] :: Load或LoadFile。
#4
I would write a Cmdlet in C# that takes a bunch of parameters and spits out the XML that you want. Then you can use that Cmdlet from Powershell by piping things into it. I hope this helps!
我会在C#中编写一个Cmdlet,它接受一堆参数并吐出你想要的XML。然后你就可以使用Powershell中的Cmdlet来管理它。我希望这有帮助!