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, 2009Environment:
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
}
相关文章推荐
- 【翻译自mos文章】当在Windows 2008 server 上安装OGG Director Server之后,Director Web无法访问
- Windows 8/8.1 Key_Windows 8.1|Windows 8|Windows 7|Windows Vista|Office 2013|Office 2010|Win Server 2012|Win Server 2008|Vista|各版
- Windows 2008 Server的企业应用机会
- Windows-Server-2008-R2安装Oracle12C数据库
- POSTED IN WINDOWS SERVER 2008 R2 -- Windows Event Triggers
- windows 2008 server R2 版本概览
- windows 2008 server 各版本概览
- List of Cluster Hotfixes for Windows Server 2008 R2
- 远程为服务器安装Windows 2008 Server
- Windows 2008 Server 故障转移群集
- Windows 2008 Server 远程 声音
- windows 2008 R2 Server无法远程桌面登陆
- MAC OS通过Ctrol+Alt+Delete登录Windows 2008 Server虚拟机
- 如何在Windows Vista and Windows Server 2008 中禁用IPv6?
- Windows Vista/Server 2008/7 OEM 激活 下载
- 在windows storage server 2008上创建iscsi磁盘 推荐
- LVS-DR,real-server为windows 2008的配置
- Vmware ESX5i 环境下部署Windows Storage Server 2008 R2
- SERVER 2008 R2 SP1下的内存虚拟盘(支持32位,64位的所有windows版本)