7

谁能帮我将 C# .NET 程序转换为 PowerShell cmdlet?我对这个领域很陌生。请帮助我离开这个检查站!

问候,

阿伦

4

3 回答 3

13

添加对System.Management.Automation的引用,创建一个继承自Cmdlet的类并覆盖ProcessRecord方法:

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

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

添加安装程序:

[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."; } }
}

安装您的命令行开关程序集:

Installutil /i myassembly.dll

并添加:

Add-PsSnapin MyCommandlets
于 2008-12-05T08:30:24.263 回答
2

There's an add-on for Reflector to do that. Here's a good article with the example: http://msmvps.com/blogs/paulomorgado/archive/2009/09/18/powershell-for-the-net-developer.aspx*

.NET Reflector has an array of add-ons on CodePlex, and one of these is the PowerShell add-on that allows you to disassemble code directly into PowerShell.

In this example, I am opening the method ChangeAccountPassword from the SPUtility library in SharePoint:

image

I can now change targets from C# to PowerShell.

image

When you need to convert helper methods from C# to PowerShell or if you’re new to PowerShell syntax, this tool is really helpful!

[*] The link is dead as of 10/12/2015.

于 2012-01-11T17:07:18.003 回答
2

首先,您应该使用 PowerShell 模板将 .cs 文件转换为 DLL。然后通过使用pssnapingetproc您可以将其转换为 DLL。

于 2008-12-12T08:03:16.030 回答