pssnapin

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time using a technique called Reflection.

For more information, see DllImportAttribute Class. 一般来讲,帮助文档与书籍的区别是书籍更系统,逻辑性强,一步一步照做可以完成。而帮助文档更详细,却没有逻辑。

在纸上作笔记太慢,特别是有时候需要抄一段代码做注释;但电脑上,如博客笔记则排版不方便。可以开发一个在pad上的程序?利用pdf技术【天真的想想】

 1 using System;
 2 using System.Management.Automation;
 3 using System.ComponentModel;
 4 namespace file_touch
 5 {
 6     [RunInstaller(true)]
 7     public class MySnapIn:PSSnapIn
 8     {
 9         public override string Name
10         {
11             get
12             {
13                 return "Wiley";
14             }
15         }
16         public override string Vendor
17         {
18             get
19             {
20                 return "Wiley_Vendor";
21             }
22         }
23         public override string Description
24         {
25             get
26             {
27                 return "This is a snap-in sample";
28             }
29         }
30     }
31 
32     [Cmdlet(VerbsCommunications.Write,"Hi")]
33     public class SayHi:Cmdlet
34     {
35         protected override void ProcessRecord()
36         {
37             WriteObject("Hi,World");
38         }
39     }
40     //[Cmdlet(VerbsCommunications.Write,"Hello") ]
41     public class SayHello:Cmdlet
42     {
43         protected override void ProcessRecord()
44         {
45             WriteObject("Hello,World");
46         }
47     }
48 }

上面的是第一个powershell的开发程序。开发了两个非常简单的cmdlet。需要先编译成dll文件(通过vs2008环境,可能需要手动加入automation链接库,与更新install库)再通过installutil注册,通过add-pssnapin加载到powershell中,就可以调用。

其中,installutil的用法:installutil *.dll(-u参数u反注册)

通过get-pssnapin -registered可以得到已经注册的snapin的名字。(相应的remove-pssanpin完成删除)

add-pssnapin (snapin的名字)来加载。

另外,程序中的方括号为c#语言的属性。当把第6行属性注释,则程序的dll文件无法完成注册;当把40行注释,则helloworld类没有生成cmdlet,通过get-command Write_h*发现,只生成了Write-hi 。

原文地址:https://www.cnblogs.com/xtypeu/p/3148704.html