http://www.thescriptlibrary.com/default.asp?Action=SubIndex&ScriptLanguage=VBScript
不过现在还不太清楚如何在一个vbscript调用另外一个vbscript文件里的函数呢?
需要研究一下
To see a world in a grain of sand.
And a heaven in a wild flower
Hold infinity in the palm of your hand.
And eternity in an hour
一花一世界
一沙一天堂
掌中握无限
霎那成永恒
星期五, 十月 26, 2007
星期二, 十月 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
}
当进入管理界面通过.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之前清空的方式。
经过努力,发现没有其他办法实现。
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
必须是通过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就可以了。
其他命令的输出需要同管道|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
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')
$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]
}
[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
##
## ie:
##
## ## Prepare the parameter
## CreateHardLink function
## $parameterTypes = [string],
## $parameters = [string]
##
## ## Call the CreateHardLink
## $result = Invoke-WindowsApi
## $parameterTypes
##
##############################
param(
[string] $dllName,
[Type] $returnType,
[string] $methodName,
[ Type[]] $parameterTypes,
[Object[]] $parameters
)
## Begin to build the dynamic
$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
## we clone the user's inputs
## the P/Invoke call.
$inputParameters = @()
$refParameters = @()
for($counter = 1; $counter -le $parameterTypes.Length; $counter++)
{
## If an item is a PSReference
## wants an [out] parameter.
if($parameterTypes[$counter - 1] -eq [Ref])
{
## Remember which parameters
$refParameters += $counter
## On the cloned array, we
## .Net reference type that
## and the value with the
$parameterTypes[ $counter - 1] =
$parameters[$counter - 1 ].Value.GetType().MakeByRefType
$inputParameters += $parameters[$counter - 1].Value
}
else
{
## Otherwise, just add their
## input array.
$inputParameters += $parameters[$counter - 1]
}
}
## Define the actual P/Invoke
## attribute for any parameters
## parameters.
$method = $type.DefineMethod( $methodName, 'Public,HideBySig,Static
$returnType, $parameterTypes )
foreach($refParameter in $refParameters)
{
}
## Apply the P/Invoke construct
$ctor = [Runtime.InteropServices.DllImportAttribute ].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttribute
$method.SetCustomAttribute($attr)
## Create the temporary type,
$realType = $type.CreateType()
$inputParameters)
## Finally, go through all of
## values of the PSReference
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中查看日志内容
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
才寻找到了一些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毙了。
下载地址: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();
}
}
一下只写明增强版本的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成功了。
编译一个以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成功了。