星期三, 一月 16, 2008

Redhat 如何打开 telnet

主要是iptable的负作用。
需要如此
/etc/init.d/./ipchains stop
/etc/init.d/./xinetd restart //重起xinetd

如果需要允许root远程登录,修改
/etc/securetty
加入:
pts/0,pts/1



星期六, 十二月 22, 2007

罗马数字的表示方法

I - 1 unus
II - 2 duo
III - 3 tres
IV - 4 quattuor
V - 5 quinque
VI - 6 sex
VII - 7 septem
VIII - 8 octo
IX - 9 novem
X - 10 decem
XI - 11 undecim
XII - 12 duodecim
XIII - 13 tresdecim
XIV - 14 quattuordecim
XV - 15 quindecim
XVI - 16 sedecim
XVII - 17 septendecim
XVIII - 18 duodeviginti
XIX - 19 undeviginti
XX - 20 viginti
XXI - 21 viginti unus
XXII - 22 viginti duo
XXVIII - 28 duodetriginta
XXIX - 29 undetriginta
XXX - 30 triginta
XL - 40 quadraginta
L - 50 quinquaginta

1~10: I II III IV V VI VII VIII IX X
11~20: XI XII XIII XIV XV XVI XVII XVIII XIX XX
21~30: XXI XXII XXIII XXIV XXV XXVI XXVII XXVIII XXIX XXX
31~40: XXXI XXXII XXXIII XXXIV XXXV XXXVI XXXVII XXXVIII XXXIX XL
41~50: XLI XLII XLIII XLIV XLV XLVI XLVII XLVII XLIX L
其中,I 为1
V为5
X为10
L为50

星期五, 十二月 21, 2007

Hibernate的数据库事务处理

1. 介绍数据库事务、事务隔离级别、悲观锁、乐观锁等概念。

2.数据库ACID特征:

Atomic(原子性):指整个数据库事务是不可分割的工作单元。

Consistency(一致性):指数据库事务不能破坏关系数据的完整性以及业务逻辑上的一致性。

Isolation(隔离性):指的是在并发环境中,当不同的事务同时操纵相同的数据时,每个事务都有各自的完整数据空间。

Durability(持久性):指的是只要事务成功结束,它对数据库所作的更新就必须永久保存下来。

3.数据库系统支持两种事务模式:

自动提交模式:每个SQL语句都是一个独立的事务,当数据库系统执行完一个SQL语句后,会自动提交事务。

手动提交模式:必须由数据库客户程序显示指定事务开始边界和结束边界。

4.MySQL中数据库表分为3种类型:INNODB、BDB和MyISAM,其中MyISAM不支持数据库事务。MySQL中create table 语句默认为MyISAM类型。

5.对于同时运行的多个事务,当这些事务访问数据库中相同的数据时,如果没有采取必要的隔离机制,就会导致各种并发问题,这些并发问题可归纳为以下几类:

A.第一类丢失更新:撤销一个事务时,把其他事务已提交的更新数据覆盖。

B.脏读:一个事务读到另一个事务为提交的更新数据。

C.虚读:一个事务读到另一个事务已提交的新插入的数据。

D.不可重复读:一个事务读到另一个事务已提交的更新数据。

E.第二类丢失更新:这是不可重复读中的特例,一个事务覆盖另一个事务已提交的更新数据。

6.数据库系统提供了四种事务隔离级别供用户选择:

A.Serializable(串行化):一个事务在执行过程中完全看不到其他事务对数据库所做的更新。

B.Repeatable Read(可重复读):一个事务在执行过程中可以看到其他事务已经提交的新插入的记录,但是不能看到其他其他事务对已有记录的更新。

C.Read Commited(读已提交数据):一个事务在执行过程中可以看到其他事务已经提交的新插入的记录,而且能看到其他事务已经提交的对已有记录的更新。

D.Read Uncommitted(读未提交数据):一个事务在执行过程中可以拷打其他事务没有提交的新插入的记录,而且能看到其他事务没有提交的对已有记录的更新。

隔离级别越高,越能保证数据的完整性和一致性,但是对并发性能的影响也越大。对于多数应用程序,可以有优先考虑把数据库系统的隔离级别设为Read Commited,它能够避免脏读,而且具有较好的并发性能。尽管它会导致不可重复读、虚读和第二类丢失更新这些并发问题,在可能出现这类问题的个别场 合,可以由应用程序采用悲观锁或乐观锁来控制。

7.当数据库系统采用read Commited隔离级别时,会导致不可重复读喝第二类丢失更新的并发问题,可以在应用程序中采用悲观锁或乐观锁来避免这类问题。从应用程序的角度,锁可以分为以下几类:

A.悲观锁:指在应用程序中显示的为数据资源加锁。尽管能防止丢失更新和不可重复读这类并发问题,但是它会影响并发性能,因此应该谨慎地使用。

B.乐观锁:乐观锁假定当前事务操作数据资源时,不回有其他事务同时访问该数据资源,因此完全依靠数据库的隔离级别来自动管理锁的工作。应用程序采用版本控制手段来避免可能出现的并发问题。

8.悲观锁有两种实现方式:

A.在应用程序中显示指定采用数据库系统的独占所来锁定数据资源。SQL语句:select ... for update,在Hibernate中使用get,load时如session.get(Account.class,new Long(1),LockMode,UPGRADE)

B.在数据库表中增加一个表明记录状态的LOCK字段,当它取值为“Y”时,表示该记录已经被某个事务锁定,如果为“N”,表明该记录处于空闲状态,事务可以访问它。增加锁标记字段就可以实现。

9.利用Hibernate的版本控制来实现乐观锁

乐观锁是由程序提供的一种机制,这种机制既能保证多个事务并发访问数据,又能防止第二类丢失更新问题。

在应用程序中可以利用Hibernate提供的版本控制功能来视线乐观锁,OR映射文件中的元素和都具有版本控制的功能,一般推荐采用

星期三, 十二月 19, 2007

VBA.Vbscript的定时器timer.任务安排功能的实现

方法:
Application.
OnTime(EarliestTime, Procedure, LatestTime, Schedule)
因为可以有许多的任务,所以默认的索引任务的唯一key,就是该任务开始的时间。就是:
EarliestTime,如果想可以控制一个任务,必须保留下来开始任务的时间。
vba中需要申明一个全局的public变量来保存,
例子:
Public RunWhen As Double

找一个地方调用下面的方法就可以了。

sub msgMovie
RunWhen = Now + DateTime.TimeValue("00:00:05")
Application.OnTime RunWhen, "msgMovie"
end sub

如果要停止这个任务,需要调用以下方法:
sub stopTime()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:="msgMovie", Schedule:=False
end Sub

VBA/Vbscript 如何彻底关闭一个UserForm实例

Excel里的VBA定义了一个UserForm
当点击close按钮的时候,往往是调用me.hide方法来隐藏。单这样不能完全清楚当前
实例,当再次调用me.show的时候,只是将该窗口重新显示。
所以,如果需要在隐藏窗口的时候动态修改了内容,当再次显示的时候,
想显示出来的时候,必须加入unload me代码
如下:
Private Sub okbutton_Click()
aboutForm.Hide
Unload Me
End Sub

星期一, 十一月 19, 2007

Mathematica 6 帮助搜索没有反应

原因:

Mathematica 6 用Java打开连接服务器来动态更新帮助文档,如果在有了firewall或proxy的情况下,Mathematica 6 可能会应为等待返回而造成,帮助的搜索,点击link无法打开新文档新窗口。

这种情况下要关闭Mathematica 6的这个选择。

在edit.Preferencs..Internet Connectivity去掉这个功能。

如图

星期五, 十一月 09, 2007

配置Jboss web 虚拟目录

在jboss 4.2.1 GA里需要修改
deploy/jboss-web.deployer/server.xml
加入下面类似的红色代码行。

<Host name="localhost"
autoDeploy="false" deployOnStartup="false" deployXML="false"
configClass="org.jboss.web.tomcat.security.config.JBossContextConfig"
>
......

<context path="/help" docbase="/home/jb/jboss/server/mrp_guide" debug="0" reloadable="true">
</host >

星期五, 十月 26, 2007

网络Vbscript函数库大全

http://www.thescriptlibrary.com/default.asp?Action=SubIndex&ScriptLanguage=VBScript

不过现在还不太清楚如何在一个vbscript调用另外一个vbscript文件里的函数呢?

需要研究一下

星期二, 十月 23, 2007

Sqlite3 3.5.1 如何读取汉字?

首先.sqlite3 建立的数据库默认编码是UTF-8.
当进入管理界面通过.read sql.txt运行的命令或insert into插入的汉字,sqlite3认为是"合格"的
UTF-8字符串。

所以需要,使用UltraEdit之类的软件,首先把sql.text转换为标准的utf-8的编码,然后再用.read读取

在powershell读取sqlite.net的数据库代码:
1.首先需要建立一个数据库,名字叫szsk.db3,只要在sqlite3.exe 后面跟随数据库名就可以了。
sqlite3.exe szsk.db3 ,然后敲入.quit推出,就发现szsk.db3已经被建立了。
sqlite3.exe可以从www.sqlite.org下载最新的window 版本。

把System.Data.SQLite.dll文件放在当前目录下.
SQLITE.NET 2.0 下载地址: http://sourceforge.net/projects/sqlite-dotnet2
cls
$global:sdb="$pwd\szsk.db3"
$global:url="$pwd\System.Data.SQLite.dll"
$createTable=@"
CREATE TABLE IF NOT EXISTS DoubleBall (
id integer primary key AUTOINCREMENT,
_Year varchar(4),
_Month varchar(2),
_Day varchar(2),
_Term varchar(3),
_N1 varchar(2),
_N2 varchar(2),
_N3 varchar(2),
_N4 varchar(2),
_N5 varchar(2),
_N6 varchar(2),
_Blue varhcar(2)
)
"@
$dropit= "drop table doubleball"

$command="";
trap [exception]
{
write-host "Exception"
}
function initdb
{
[void][system.reflection.Assembly]::LoadFile($url)
if ((test-path $sdb) -eq $FALSE)
{
$global:conn=new-object System.Data.SQLite.SQLiteConnection("data source="+$sdb+";Version=3;New=TRUE;")
}else
{
$global:conn=new-object System.Data.SQLite.SQLiteConnection("data source="+$sdb+";Version=3;New=false;")
}
}
function getDBConnection
{
$conn=new-object System.Data.SQLite.SQLiteConnection("data source="+$sdb+";Version=3;New=false;")
}
initdb

$command=new-object System.Data.SQLite.SQLiteCommand

if($args.count -eq 0)
{
return $conn
}else
{
$sql="select * from stocklist where stockCode='" + $args[0]+".sz'"
$command.CommandText =$sql

$command.Connection=$conn

$conn.open()
$dataReader =$command.ExecuteReader()
$script:cnName=$args[0]
while($dataReader.Read() )
{
$script:cnName $dataReader.GetString(1)
}
$dataReader.close()
$conn.close()
return $cnName
}

星期五, 十月 19, 2007

Vbscript 实现 cls命令

目的:在Vbscript中实现清空当前cmd窗口的内容。经过实验 发现一下代码可以实现。

Set WshShell = CreateObject("wscript.shell")
WshShell.SendKeys "%( )+E+P{ENTER}CLS{ENTER}{TAB}",True
Wscript.Sleep 5000

但是这个代码有一个问题就是键盘缓冲区是在当前VBS执行完成后,才被发送到当前cmd
窗口,因此是一种后script方式,不是希望在执行其他vbscript之前清空的方式。

经过努力,发现没有其他办法实现。


星期三, 十月 17, 2007

如何在vbscript/cscript 中调用.dll

1.普通DLL
必须是通过regsvr32 可以注册的dll才能在vbscript中被调用,应为createobject必须是 ActiveObject dll,如果是标准的函数库,则无法使用。必须包装成Activexobject

2..NET Dll
必须通过 regasm注册后,就可以在vbscript中使用了?

regasm System.Data.SQLite.DLL /tlb:System.Data.SQLite.tlb /codebas

星期三, 十月 10, 2007

Powershell .ps1 如何得到返回的对象?

把需要返回的对象在ps1文件的最后一行写明就可以了。
其他命令的输出需要同管道|out-null,来回避。

比如下面:
[system.reflection.Assembly]::LoadFrom($url) |out-null

$conn=new-object System.Data.SQLite.SQLiteConnection("data source="+$sdb+";Version=3;New=TRUE;")

如果想返回一个connection object
就在ps1文件的最后一行写明
$conn就可以了。

星期一, 十月 08, 2007

PowerShell如何处理HTMLDocument Object

上次写的文章中因为无法使用Com对象“HTMLFILE”,而不得不采用IE.Application对象来处理
HTMLDocument对象。
但是速度上同同样的VBS代码比感觉差太多。所以始终想使用"HTMLFILE"来处理HTML Document.

最终通过get-member发现了 在PowerShell里使用的IHTMLDocument2_write方法可以用来初始化HTMLDocument对象内容。

如下代码,可以在POWERSHELL里使用了。

$htmlDoc= New-Object -com "HTMLFILE"
$page = "<html><body>this is a test</body></html>"
$htmlDoc.IHTMLDocument2_write($page)
$htmlDoc.close

如何在PowerShell调用Dll文件。

PowerShell中可以直接使用.NET的Dll库,通过如下语句:
[system.reflection.Assembly]::LoadFrom("dll所在绝对路径");

但是传统的非.NET生成的Dll库如何使用呢?
比较复杂,基本可以按照下列语句来使用:
$dir="C:\\curl-7.16.4\\FTCTRLPRO3.dll"
$ctor = [Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, $dir

这只能简单载入Dll,如果想使用Dll的具体的函数,可以参看如下文章:
http://www.leeholmes.com/blog/ManagingINIFilesWithPowerShell.aspx
##############################################################################
##
## Invoke-WindowsApi.ps1
##
## From PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
## Invoke a native Windows API call that takes and returns simple data types.
##
## ie:
##
## ## Prepare the parameter types and parameters for the
## CreateHardLink function
## $parameterTypes = [string], [string], [IntPtr]
## $parameters = [string] $filename, [string] $existingFilename, [IntPtr]::Zero
##
## ## Call the CreateHardLink method in the Kernel32 DLL
## $result = Invoke-WindowsApi "kernel32" ([bool]) "CreateHardLink" `
## $parameterTypes $parameters
##
##############################################################################

param(
[string] $dllName,
[Type] $returnType,
[string] $methodName,
[ Type[]] $parameterTypes,
[Object[]] $parameters
)

## Begin to build the dynamic assembly
$domain = [AppDomain]::CurrentDomain
$name = New-Object Reflection.AssemblyName 'PInvokeAssembly'
$assembly = $domain.DefineDynamicAssembly( $name, 'Run')
- Show quoted text -
$module = $assembly.DefineDynamicModule( 'PInvokeModule')
$type = $module.DefineType('PInvokeType', "Public,BeforeFieldInit")

## Go through all of the parameters passed to us. As we do this,
## we clone the user's inputs into another array that we will use for
## the P/Invoke call.
$inputParameters = @()
$refParameters = @()

for($counter = 1; $counter -le $parameterTypes.Length; $counter++)
{
## If an item is a PSReference, then the user
## wants an [out] parameter.
if($parameterTypes[$counter - 1] -eq [Ref])
{
## Remember which parameters are used for [Out] parameters
$refParameters += $counter

## On the cloned array, we replace the PSReference type with the
## .Net reference type that represents the value of the PSReference,
## and the value with the value held by the PSReference.
$parameterTypes[ $counter - 1] =
$parameters[$counter - 1 ].Value.GetType().MakeByRefType()
$inputParameters += $parameters[$counter - 1].Value
}
else
{
## Otherwise, just add their actual parameter to the
## input array.
$inputParameters += $parameters[$counter - 1]
}
}

## Define the actual P/Invoke method, adding the [Out]
## attribute for any parameters that were originally [Ref]
## parameters.
$method = $type.DefineMethod( $methodName, 'Public,HideBySig,Static,PinvokeImpl',
$returnType, $parameterTypes )
foreach($refParameter in $refParameters)
{
[void] $method.DefineParameter($refParameter, "Out", $null)
}

## Apply the P/Invoke constructor
$ctor = [Runtime.InteropServices.DllImportAttribute ].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, $dllName
$method.SetCustomAttribute($attr)

## Create the temporary type, and invoke the method.
$realType = $type.CreateType()

$realType.InvokeMember($methodName, 'Public,Static,InvokeMethod', $null, $null,
$inputParameters)

## Finally, go through all of the reference parameters, and update the
## values of the PSReference objects that the user passed in.
foreach($refParameter in $refParameters)
{
$parameters[$refParameter - 1].Value = $inputParameters[$refParameter - 1]
}

星期五, 十月 05, 2007

Akashic records

空之境界中对于阿克夏记录,或者说阿卡沙年代记的注解十分简单和模糊。阿克夏记录(Akashic Records)并不具备电脑分析和处理信息的功能,它所能做的只有忠实详细的记录而已。

阿 克夏的概念起源于印度。在婆罗门教的文献中,“阿克夏”被描述为“宇宙的精神—物质”,它包括了所有的存在与发展。与之类似的“梵”(婆罗门教与印度教的 主神之一,为创造之神,亦指终生之本)在实质上通常被视作非个人的、无法识别的最高宇宙原理,万物来源于它,万物又回归于它。它非实体、非物质、非先天而 又永恒存续,无始无终。按照古老的印度学说,梵“能够穿透一切,赋予至高无上的天神或是小到极点的矿物原子以生机。”古印度秘密学说最后明确指出,“阿克 夏就是一切创造物的起源于回归之地”,阿克夏“比一切万物都更古老”,简直就是“最后的终点”。阿克夏就是世间万物的根源(荒耶宗莲竭尽全力所追求的,想 必就是这个东西了);而阿克夏记录则使得任何时刻——包括过去、现在和未来——所发生的任何事情都不致归于遗忘之中,它记录了一切的信息。

当 然在客观上,没有确切的证据能证明这样一种所谓的“宇宙记忆”确实存在,或者证明这个世界上从一开始曾经发生的一切都被记录了下来。但若仅仅因此就认为阿 克夏记录是不存在的,同样不能令人信服。即使是现代,这种现象也能得到共鸣和认同,只是换了一个完全不同的名字:形态发生场。

剑桥大学的 生化学家鲁伯特·谢尔德雷克在其著作《创造性的宇宙》中写道,形态发生场能够摆脱时间和空间发挥作用。具体地说,如果一个生物种类的成员获得了一种新的行 为,那么它自己的形态发生场会由此改变。只要这种新的行为在足够长的时间里得到了坚持,物种成员之间就会形成一种相互影响,进而影响整个物种。实际的例子 是:一只老鼠一旦学会了在一明一暗的信号下告到黄油,其他老鼠就会越来越快地掌握这个方法。直到最后,所有的老鼠——即便不是由最初的那只生出来的,也不 曾与它有过接触——都学会了这种行为。

按照谢尔德雷克的观点,自然是具有“记忆”的,我们通常视作自然法则的东西只不过是“习惯”。自然存在与“习惯”也就是“行为模式”之中。根据这一构想,自然界中的每一类事物都必须按照它那一类的“集体记忆”内容来行事,从有生命的事物到无生命的物质都是如此。

调试.NET错误工具

再运行Powershell经常出现一些系统错误,可以通过
C:\Program Files\Microsoft.NET\SDK\v2.0\Bin\FUSLOGVW.exe 来观察错误。
首先运行FUSLOGVW.exe ,然后再运行Powershell,就可以在FUSLOGVW.exe窗口中看到错误内容。如图


双击条目或按下 查看日志按钮,则在IE中查看日志内容

Dll 函数查看工具

因为在Powershell里调用一些dll,总是出现错误。
才寻找到了一些Dll函数查看器。
都挺不错的。
DLL Export Viewer
http://www.nirsoft.net/utils/dll_export_viewer.html

国产的。
http://www.onlinedown.net/soft/17471.htm

星期四, 十月 04, 2007

在Powershell里访问Mysql 5.0 数据库

首先必须安装了Mysql 5.0 Connector .net 5.0的driver,
下载地址:http://www.mysql.org/downloads/connector/net/5.0.html
然后编写如下代码。
[void][system.reflection.Assembly]::LoadFrom("D:\mysql\MySQL Connector Net 5.0.8.1\Binaries\.NET 2.0\MySQL.Data.dll")
$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "server=localhost;user id=root;password=root;database=mrpII;pooling=false"
$myconnection.Open()
$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$mycommand.Connection = $myconnection
$mycommand.CommandText = "SELECT * from MRP_BOM"

$adapter = new-object MySql.Data.MySqlClient.MySqlDataAdapter
$adapter.SelectCommand = $mycommand
$ds = new-object System.Data.DataSet
$adapter.Fill($ds)
$myconnection.close()
$ds.Tables[0] |Format-Table #非常Cool的输出效果,如下图

真是cool毙了。

如何传递一个参数到cmdlet?

增强版本的HelloWorld,通过传递一族人民到HellowWorld.
一下只写明增强版本的cmdlet程序,注册的snapin没有变化。
HelloWorld.cs
在PS下通过 get-helloworld,一个接一个输入所有名称,回车表示输入结束。
或者通过get-helloworld ("William wang","Peter Zhang","李华") 来直接得到结果.


using System;
using System.Management.Automation;


[Cmdlet(VerbsCommon.Get, "HelloWorld", SupportsShouldProcess = true)]
public class HelloWorld : PSCmdlet
{

// An array is used to support the following scenario:
[Parameter(
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Mandatory = true,
HelpMessage = "恭贺的名字清单")]
[ValidateNotNullOrEmpty]
public string[] FullName
{
get { return fullName; }
set {fullName = value; }
}
private string[] fullName;
// Called when cmdlet is invoked.
protected override void BeginProcessing()
{
// You could open a database connection here for example.
base.BeginProcessing();
}

// Called when we have pipeline input.
protected override void ProcessRecord()
{
// this.Name could be null in BeginProcessing (no parameters passed)
// but could be filled when ProcessRecord gets called (parameters from pipeline)
try
{
if(this.fullName==null)
{
WriteObject("fullName is null");
}else
{

foreach (string item in this.fullName)
{
WriteObject("Hello World!"+item);
}
}

}catch(Exception)
{
WriteObject("excetpin");
}
}

// Called after every record has been processed.
protected override void EndProcessing()
{
base.EndProcessing();
}

// Called when the user hits CTRL+C
protected override void StopProcessing()
{
base.StopProcessing();
}
}

编写Powershell的HelloWorld的例子,开始编写cmdlet第一步

首先来自Microsoft的例子:http://msdn2.microsoft.com/en-us/library/ms714437.aspx
编译一个以C#语言编写的cmdlet需要的开发环境,可以参看:
http://msdn2.microsoft.com/en-us/library/ms714644.aspx

首先要确认安装了.NET 2.0 Framework的SDK包含csc.exe c#的命令行编译软件。
如果安装成功了.net 2.0 framework SDK,可以在一下目录发现:
C:\WINDOWS\Microsoft.NET\Framework 发现所有安装的.NET Framework的目录。
.Net framework SDK 3.0是在.NET 2.0 SDK的基础上增加了一些新的特性完成的。
所以.NET 3.0自身不带有csc.exe编译软件。
C:\WINDOWS\Microsoft.NET\Framework的典型目录如下。
<dir> v1.0.3705
<dir> v1.1.4322
<dir> v2.0.50727 csc.exe所在目录
<dir> v3.0

其次需要Powershell SDK的安装,如果安装成功可以发现如下目录。
C:\Program Files\Reference Assemblies\Microsoft 发现powershell目录
包含一些.dll 库.

C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0
2006-09-28 15:49 139,264 Microsoft.PowerShell.Commands.Management.dll
2006-09-28 15:49 294,912 Microsoft.PowerShell.Commands.Utility.dll
2006-09-28 15:49 200,704 Microsoft.PowerShell.ConsoleHost.dll
2006-09-28 15:49 65,536 Microsoft.PowerShell.Security.dll
2006-09-28 15:49 1,564,672 System.Management.Automation.dll

软后编写 用来编译C#的Powershell批处理文件,需要包含命令行编译C#代码的一些参考库
cscc.bat包含内容:

set PS_HOME="C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0"
csc.exe /reference:%PS_HOME%\Microsoft.PowerShell.Commands.Management.dll,%PS_HOME%\Microsoft.PowerShell.Commands.Utility.dll,%PS_HOME%\Microsoft.PowerShell.ConsoleHost.dll,%PS_HOME%\Microsoft.PowerShell.Security.dll,%PS_HOME%\System.Management.Automation.dll /t:library %1 %2 %3
%1 %2 %3 代表 C#的代码文件.cs文件。

编写HelloWorld的cmdlet步骤。
1.首先编写cmdlet或pscmdlet的子类:
HelloWorld.cs:
using System;
using System.Management.Automation;


[Cmdlet(VerbsCommon.Get, "HelloWorld", SupportsShouldProcess = true)]
public class HelloWorld : PSCmdlet
{

// Called when cmdlet is invoked.
protected override void BeginProcessing()
{
// You could open a database connection here for example.
base.BeginProcessing();
}

// Called when we have pipeline input.
protected override void ProcessRecord()
{
// this.Name could be null in BeginProcessing (no parameters passed)
// but could be filled when ProcessRecord gets called (parameters from pipeline)
try
{
WriteObject("Hello World!");
}catch(Exception)
{

}
}

// Called after every record has been processed.
protected override void EndProcessing()
{
base.EndProcessing();
}

// Called when the user hits CTRL+C
protected override void StopProcessing()
{
base.StopProcessing();
}
}

2.再编写一个Snapin的注册类。
HelloWorldSnapin.cs

using System;
using System.ComponentModel;
using System.Management.Automation;


[RunInstaller(true)]
public class HelloWorldSnapIn : PSSnapIn
{
public override string Name
{
get { return "HelloWorldSnapIn"; }
}

public override string Vendor
{
get { return "William Wang"; }
}

public override string Description
{
get { return "This Windows PowerShell snap-in contains the Get-HelloWorld cmdlet."; }
}
}

3.编译:
set PS_HOME="C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0"
csc.exe /reference:%PS_HOME%\Microsoft.PowerShell.Commands.Management.dll,%PS_HOME%\Microsoft.PowerShell.Commands.Utility.dll,%PS_HOME%\Microsoft.PowerShell.ConsoleHost.dll,%PS_HOME%\Microsoft.PowerShell.Security.dll,%PS_HOME%\System.Management.Automation.dll /t:library HelloWorld*.cs

得到 HelloWorld.dll文件,然后安装
InstallUtil.exe HelloWorld.dll得到如下成功message:
正在运行事务处理安装。

正在开始安装的“安装”阶段。
查看日志文件的内容以获得 C:\curl-7.16.4\helloworld.dll 程序集的进度。
该文件位于 C:\curl-7.16.4\helloworld.InstallLog。
正在安装程序集“C:\curl-7.16.4\helloworld.dll”。
受影响的参数是:
logtoconsole =
assemblypath = C:\curl-7.16.4\helloworld.dll
logfile = C:\curl-7.16.4\helloworld.InstallLog

“安装”阶段已成功完成,正在开始“提交”阶段。
查看日志文件的内容以获得 C:\curl-7.16.4\helloworld.dll 程序集的进度。
该文件位于 C:\curl-7.16.4\helloworld.InstallLog。
正在提交程序集“C:\curl-7.16.4\helloworld.dll”。
受影响的参数是:
logtoconsole =
assemblypath = C:\curl-7.16.4\helloworld.dll
logfile = C:\curl-7.16.4\helloworld.InstallLog

“提交”阶段已成功完成。

4.然后在powershell里注册这个cmdlet
add-pssnapin helloworldsnapin
如果成功,没有任何提示。


5.运行get-helloworld
得到 hello world!的提示信息。

表明最简单的Hello World cmdlet成功了。