3

I have developed a powershell module in C#, implemented a few commands.

How can I execute C# code in this module when it's imported by Powershell?

2
  • You want you binary(c#) powershell-module to run some code when it's imported? Unclear question. Have you tried anything yet? Commented Jan 30, 2013 at 18:34
  • 1
    Correct, I want to execute C# code in my module when it's imported by powershell. I want to do some initiation stuff. And "when it's imported by powershell" -> when executing command: Import-Module .\MyModule.dll Commented Jan 30, 2013 at 18:39

3 Answers 3

6

Create a module manifest with the ModuleToProcess (or RootModule in V3) field set to the PSM1 file and the NestedModules set to the DLL e.g.:

RootModule         = 'Pscx.psm1'
NestedModules      = 'Pscx.dll' 

This is what we do in the PowerShell Community Extensions where we do the same thing - fire up a script first. You can see our PSD1 file here.

Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to sign the assembly with signtool, or how does the psd1 file become embedded into the dll file?
You don't embed the PSD1 in the DLL. You ship a module as a collection of files in a folder e.g. PSCX\pscx.psd1, PSCX\pscx.psm1, PSCX\pscx.dll, etc. And you don't have to Authenticode sign anything.
I use exactly this approach, but then the module auto-loading does not work. My users must explicitly run Import-Module before first use. When I keep RootModule empty, auto-loading just works. Do you have the same problem?
1

I'm also writing a binary cmdLet in .NET. I have found that if you create a class that inherits from at least DriveCmdletProvider, that class can implement InitializeDefaultDrives.

This method will get call when import-module is called on your DLL.

You could use this 'feature' to stand up some session (or module session) data.

Comments

0

This is a very basic solution, simply replace code within the {} with your source. (my test below)

add-type 'public class c{public const string s="Hello World";}';[c]::s

enjoy

4 Comments

Are you suggesting to explicit execute a function after import?
I'm not a c# developer by any stretch of the imagination. But when I write my powershell modules when you import the module the code not wrapped in a function or workflow is executed as soon as it loads. I cannot be sure this is the case with a C# module.
I don't think there's anything that corresponds to trailing script code in a C# module.
Keith Hill has it on the nose, ModuleToProcess, my first logical step would have been script to process, this is an excerpt from my Module Manifest. # Script files (.ps1) that are run in the caller's environment prior to importing this module. # ScriptsToProcess = @()

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.