How to convert .cs file to a PowerShell cmdlet?

后端 未结 3 1103
离开以前
离开以前 2021-02-06 09:21

Can anyone help me to convert a C# .NET program to PowerShell cmdlet? I am very new to this area. Please help me to get out of this checkpoint!

Regards,

Arun

3条回答
  •  礼貌的吻别
    2021-02-06 09:38

    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
    

提交回复
热议问题