How to develope Cmdlet in vs2008 under windows server 2008

How to Develop cmdlets under 2008

Andrew, March 10, 2009

Environment:

Windows Server 2008 build 6001,sp1.

VisualStudio 2008 pro.

Powershell V2(CTP2).

 

Steps:

1.       Install Windows SDK from MSDN, or from intranet. Windows SDK contains PowerShell SDK.

2.       new c# console project “MyCmdlet”. Sample code

3.       add System.Configuration.Install to References.

4.       add c:/Program Files/Reference Assemblies/Microsfot/WindowsPowerShell/v1.0/System.Management.Automation.dll to reference.

5.       Import namespace,
using System.Management.Automation; //”c:/Program Files/Reference Assemblies/Microsoft/WindowsPowerShell/v1.0/System.Managment.Automation.dll”
using System.CompenentModel;

6.       Add Cmdlet Attribute to your class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Management.Automation;

using System.ComponentModel;

 

namespace MyCmdlet

{

    #region MyCmdlet

    [Cmdlet("get","MyInfo")]  // “get-myinfo” is the name of your cmdlet

    public class MyCmdlet

    {

 

    }

    #endregion

}

 

 

7.       Override input method. BeginProcessing, ProcessRecord,EndProcessing.

#region MyCmdlet

    [Cmdlet("get","MyInfo")]

    public class MyCmdlet :Cmdlet

    {

        protected override void ProcessRecord() // this is the key point to cmdlet

        {

 

            WriteObject("JiahuiTeam is a greate team", true);

        }

 

    }

 

 

8.       Add Code for pssnapin

 

    [RunInstaller(true)] //Use RunInstallerAttribute1

    public class MyCmdletPSSnapin : PSSnapIn

    {

        public MyCmdletPSSnapin() //An empty constructor2

            : base()

        {

        }

 

 

        public override string Name //Override Properties3

        {

            get {

                return "MyCmdlet";

            }

        }

 

        public override string Vendor //Override Properties3

        {

            get {

                return "Microsoft Jiahui Team";

            }

        }

 

        public override string VendorResource //Override Properties3

        {

            get

            {

                return "GetProcPSSnapIn01,Microsoft";

            }

        }

 

        public override string Description //Override Properties3

        {

            get {

                return "This is Demo of Cmdlet Dev";

            }

        }

 

        public override string DescriptionResource //Override Properties3

        {

            get

            {

                return "Description Resource:sdfjskdjfsdlfjdslfjdslkfjdslkfjdslfjsdljkfsdjklfjdso";

            }

        }

    }

 

9.       Register pssnapin.Run following code

 

$ref = "c:/Program Files/Reference Assemblies/Microsoft/WindowsPowerShell/v1.0/System.Management.Automation.dll"

$compiler = "$env:windir/Microsoft.NET/Framework/v3.5/csc"  #do not use v2.0.5027, it doesn't contains Linq

&$compiler /target:library /r:$ref MyCmdlet.cs

set-alias installutil $env:windir/Microsoft.NET/Framework/v2.0.50727/installutil

installutil MyCmdlet.dll

 

Or

 

project->Properties->Application->Output type = Class Library.

Compile your project, you will get MyCmdlet.dll, then run installutil.exe:

set-alias installutil $env:windir/Microsoft.NET/Framework/v2.0.50727/installutil

installutil MyCmdlet.dll

 

10.   in ps console

 

get-pssnapin –registered

add-pssnapin MyCmdlet

Get-MyInfo

 

 

More topics about dev cmdlets, please see: http://msdn.microsoft.com/en-us/library/ms714598(VS.85).aspx.

 

 

 

Sample code:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Management.Automation;

using System.ComponentModel;

 

namespace MyCmdlet

{

    #region MyCmdlet

    [Cmdlet("get","MyInfo")]

    public class MyCmdlet :Cmdlet

    {

        protected override void ProcessRecord()

        {

 

            WriteObject("JiahuiTeam is a greate team", true);

        }

 

    }

 

    #endregion

 

    #region pssnapin

    [RunInstaller(true)]

    public class MyCmdletPSSnapin : PSSnapIn

    {

        public MyCmdletPSSnapin()

            : base()

        {

        }

 

 

        public override string Name

        {

            get {

                return "MyCmdlet";

            }

        }

 

        public override string Vendor

        {

            get {

                return "Microsoft Jiahui Team";

            }

        }

 

        public override string VendorResource

        {

            get

            {

                return "GetProcPSSnapIn01,Microsoft";

            }

        }

 

        public override string Description

        {

            get {

                return "This is Demo of Cmdlet Dev";

            }

        }

 

        public override string DescriptionResource

        {

            get

            {

                return "Description Resource:sdfjskdjfsdlfjdslfjdslkfjdslkfjdslfjsdljkfsdjklfjdso";

            }

        }

    }

    #endregion pssnapin

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值