1

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.

So question is I want to wrap a c# class to enable it to be used by powershell.

I saw something some where you can refer to c# class using the square bracket mob, eg.

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.

Any help would be appreciated. Bob.

1
  • Thanks Guys, I was planning to make it into a cmdlet, but I was sure their was a way to do it via the framework classes without using powershell specifics. This will move the wee engine 1 step forward, and 1 big requirement almost completed. Thanks. Bob. Commented Aug 6, 2009 at 13:42

4 Answers 4

3

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

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 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();
Sign up to request clarification or add additional context in comments.

3 Comments

This example is using it from powershell. Is their anyway to enable the use of the assembly for all scripts, from c# when creating runspace, pipeline etc.
I updated my answer to show how to add an assembly to the runspace configuration.
Thanks Steven, Thats is pretty much what I was looking for. regards Bob.
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.

Comments

1

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.

1 Comment

This example is using it from powershell. Is their anyway to enable the use of the assembly for all scripts, from c# when creating runspace, pipeline etc.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.