您的位置:首页 > 运维架构

How to develope Cmdlet in vs2008 under windows server 2008

2009-04-02 15:15 387 查看

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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: