views:

614

answers:

2

Can anyone help me to convert a c# .net program to powershell cmdlet. I am very new to this area. plz help me to get out of this checkpoint.

Regards Arun

+8  A: 

Add a reference to System.Management.Automation, create a class that inherits from Cmdlet and override the ProcessRecord method:

[Cmdlet(VerbsCommon.Get, "Double")]
public class GetDouble : Cmdlet
{
    [Parameter]
    public int SomeInput { get; set; }

    protected override void ProcessRecord()
    {
        WriteObject(SomeInput * 2);
    }
}

Add an installer:

[RunInstaller(true)]
public class MySnapin : PSSnapIn
{
    public override string Name { get { return "MyCommandlets"; } }
    public override string Vendor { get { return "MyCompany"; } }
    public override string Description { get { return "Does unnecessary aritmetic."; } }
}

Install your commandlet assembly:

Installutil /i myassembly.dll

And add:

Add-PsSnapin MyCommandlets
Cristian Libardo
FYI: System.Management.Automation can be referenced after installing the Reference Assemblies from the Windows SDK.
Don Jones
great tip. also great answer this is exactly what i was looking for.
Anonymous Type
+2  A: 

First of all you should convert your .cs file into a dll using powershell template. Then by using pssnapin and getproc you can convert it into a dll.