当前位置: 首页 > news >正文

PowerShell的CmdLet开发的HelloWorld

导读:
  安装完windows Vista SDK后,终于可以开始CmdLet的开发了.如果安装了Samples的同学,可以直接去看示例:X:/Program Files/Microsoft SDKs/Windows/v6.0/Samples/SysMgmt/WindowsPowerShell 其中X是PS所在的安装盘.下面让偶手把手地说一下该怎么建立一个CmdLet吧:
  1.打开VS2005,创建一个windows的运行库.
  2.添加引用:C:/Program Files/Reference Assemblies/Microsoft/WindowsPowerShell/v1.0/System.Management.Automation.dll
  3.新建一个类文件,同时
  using System.Management.Automation;
  using System.ComponentModel;(这个在安装时会用到)
  4.下面开始写代码了:
  //先来完成cmdlet的实体类
  [Cmdlet(VerbsCommon.Get, "HelloWorld")] //大胆地猜测一下,PS在加载cmdlet程序集的时候,是用反射的方式来识别的,反射的时候就是靠这个attribute来实现,这里面有两个参数,第一个是动作,后一个是名字.这是cmdlet的命名方式:动词+名词
  public class ExecuteShell : Cmdlet // 继承自cmdlet的基类
  {
  private string argus;
  [Parameter(Position = 0)] //大家可以发现很有意思在这里面,随处都可以看到attribute,这里指写了第一个参数,直接就反射到类对应的属性上了.
  [ValidateNotNullOrEmpty]
  public string Args
  {
  get { return argus; }
  set { argus = value; }
  }
  protected override void ProcessRecord()
  //处理请求,我们我这里只是简单地输出一下信息.
  {
  if (argus != null &&argus.Length >0)
  {
  WriteCommandDetail("Hello World:" + argus);
  }
  }
  }
  //再来看看cmdlet的安装类
  [RunInstaller(true)] //又是这种attribute
  public class HelloWordSnapIn: PSSnapIn
  {
  ///
  /// Create an instance of the GetProcPSSnapIn01
  ///

  public PSclient()
  : base()
  {
  }
  ///
  /// Get a name for this PowerShell snap-in. This name will be used in registering
  /// this PowerShell snap-in.
  /// 注意这里面的名字最为重要在下面将要讲到
  ///

  public override string Name
  {
  get
  {
  return "HelloWordSnapIn";
  }
  }
  ///
  /// Vendor information for this PowerShell snap-in.
  ///

  public override string Vendor
  {
  get
  {
  return "BrainIron";
  }
  }
  ///
  /// Gets resource information for vendor. This is a string of format:
  /// resourceBaseName,resourceName.
  ///

  public override string VendorResource
  {
  get
  {
  return "HelloWordSnapIn,BrainIron";
  }
  }
  ///
  /// Description of this PowerShell snap-in.
  ///

  public override string Description
  {
  get
  {
  return "This is a PowerShell snap-in that includes the Get-HelloWorld cmdlet. this is a demo, design by Brian";
  }
  }
  }
  编译生成:HelloWorldCmdLet.dll
  6.这时候该安装了:使用Installutil.exe HelloWorldCmdLet.dll来把安装它.Installutil.exe如果你找不到,那么应该在SDK的BIN目录里面肯定可以找得到.
  7.这时候打开PS,使用Get-HelloWorld 命令会发现提示不支持这个命令.这时候要用:Add-PSSnapin HelloWordSnapIn 来把它注册到PS的控制台中,这个命令的后面的那个名字就是我上面说的重要的名字,而不是类名.然后再用Get-HelloWorld 命令就可以看到成果了.
  8.调试.因为程序要先注册到PS中,PS才能调用,所以好像不太好调试,其实可以用附加到进程的方式来调试.

本文转自
http://blog.joycode.com/chinahuman/archive/2007/09/18/108595.aspx

相关文章:

  • Windows PowerShell学习笔记之一——命令帮助
  • C#的String.Format格式化举例(转)
  • .net(C#)中String.Format如何使用
  • ASP.net中名称以无效字符开头问题解决?
  • log4net简易配置--
  • c开发 一个关于访问性的问题
  • FCKedit(asp.net)详细配置以及下载
  • 如何生成一个新的GUID 全球统一唯一编号
  • readonly vs. const [C#] .net
  • VS2005 常用快捷键
  • ASP.NET中IsPostBack属性
  • 模仿Gmail右上角的Loading
  • 什么是数字信封?
  • - 转 Ext2.0 form使用实例
  • Ext的layout页面布局解析[转]
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • [译] React v16.8: 含有Hooks的版本
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • Angular 响应式表单之下拉框
  • Angular6错误 Service: No provider for Renderer2
  • axios 和 cookie 的那些事
  • bearychat的java client
  • Bootstrap JS插件Alert源码分析
  • github指令
  • HTTP中GET与POST的区别 99%的错误认识
  • HTTP中的ETag在移动客户端的应用
  • JavaScript 一些 DOM 的知识点
  • java概述
  • Laravel Telescope:优雅的应用调试工具
  • Linux gpio口使用方法
  • Python代码面试必读 - Data Structures and Algorithms in Python
  • Spring Cloud中负载均衡器概览
  • ucore操作系统实验笔记 - 重新理解中断
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • 观察者模式实现非直接耦合
  • 诡异!React stopPropagation失灵
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​草莓熊python turtle绘图代码(玫瑰花版)附源代码
  • # include “ “ 和 # include < >两者的区别
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (26)4.7 字符函数和字符串函数
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (黑马C++)L06 重载与继承
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (一) springboot详细介绍
  • (转)http-server应用
  • (转)Linq学习笔记