2008-12-05 45 views
6

任何人都可以幫助我將C#.NET程序轉換爲PowerShell cmdlet嗎?我對這個領域非常陌生。請幫助我擺脫這個檢查點!如何將.cs文件轉換爲PowerShell cmdlet?

問候,

阿倫

+0

[將C#代碼轉換爲PowerShell腳本的方法?]的可能的副本(http://stackoverflow.com/questions/2143460/methods-to-convert-c-sharp-code-to-a-powershell-script ) – 2016-04-11 22:36:23

回答

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 
+0

僅供參考:System.Management.Automation可以在從Windows SDK安裝參考程序集後引用。 – 2008-12-05 15:55:51

+0

偉大的提示。也是很好的答案,這正是我所期待的。 – 2010-08-05 23:01:00

2

首先,你應該轉換.cs文件到使用PowerShell模板的DLL。然後通過使用pssnapingetproc您可以將其轉換爲DLL。

2

有一個反射器來做到這一點的附加組件。 下面是用例的好文章:http://msmvps.com/blogs/paulomorgado/archive/2009/09/18/powershell-for-the-net-developer.aspx *

.NET反射器具有an array of add-ons on CodePlex,而這些 之一是the PowerShell add-on,使您可以拆卸代碼直接 到PowerShell中。

在這個例子中,我從 的SPUtility圖書館開放的方法ChangeAccountPassword在SharePoint:

image

我現在可以改變從C#的目標,以PowerShell的。

image

當你需要的輔助方法轉換從C#到PowerShell的,或者如果 你是新來的PowerShell語法,這個工具是非常有幫助!

[*]該鏈接截至2015年12月12日已死亡。