Windows Powershell创建对象
通过new-object创建新对象
如果使用构造函数创建一个指定类型的实例对象,该类型必须至少包含一个签名相匹配的构造函数。例如可以通过字符和数字创建一个包含指定个数字符的字符串:
ps c:powershell> new-object string(‘*',100)
*******************************************************************************
*********************
为什么支持上面的方法,原因是string类中包含一个void .ctor(char, int32) 构造函数
ps c:powershell> [string].getconstructors() | foreach {$_.tostring()}
void .ctor(char*)
void .ctor(char*, int32, int32)
void .ctor(sbyte*)
void .ctor(sbyte*, int32, int32)
void .ctor(sbyte*, int32, int32, system.text.encoding)
void .ctor(char[], int32, int32)
void .ctor(char[])
void .ctor(char, int32)
通过类型转换创建对象
通过类型转换可以替代new-object
ps c:powershell> $date="1999-9-1 10:23:44"
ps c:powershell> $date.gettype().fullname
system.string
ps c:powershell> $date
1999-9-1 10:23:44
ps c:powershell> [datetime]$date="1999-9-1 10:23:44"
ps c:powershell> $date.gettype().fullname
system.datetime
ps c:powershell> $date
1999年9月1日 10:23:44
如果条件允许,也可以直接将对象转换成数组
ps c:powershell> [char[]]"mossfly.com"
m
o
s
s
f
l
y
.
c
o
m
ps c:powershell> [int[]][char[]]"mossfly.com"
109
111
115
115
102
108
121
46
99
111
109
加载程序集
自定义一个简单的c#类库编译为test.dll:
using system;
using system.collections.generic;
using system.text;
using system.net;
namespace test
{
public class student
{
public string name { set; get; }
public int age { set; get; }
public student(string name, int age)
{
this.name = name;
this.age = age;
}
public override string tostring()
{
return string.format("name={0};age={1}", this.name,this.age);
}
}
}
在powershell中加载这个dll并使用其中的student类的构造函数生成一个实例,最后调用tostring()方法。
ps c:powershell> ls .test.dll
目录: c:powershell
mode lastwritetime length name
---- ------------- ------ ----
-a--- 2012/1/13 10:49 4608 test.dll
ps c:powershell> $testdll=ls .test.dll
ps c:powershell> [reflection.assembly]::loadfile($testdll.fullname)
gac version location
--- ------- --------
false v2.0.50727 c:powershelltest.dll
ps c:powershell> $stu=new-object test.student('mosser',22)
ps c:powershell> $stu
name age
---- ---
mosser 22
ps c:powershell> $stu.tostring()
name=mosser;age=22
使用com对象
作为.net的补充,powershell可以加载和访问com对象。
查看可用的com对象
每一个com对象都有存储在注册表中的唯一标识符,想遍历访问可用的com对象,可是直接访问注册表。
dir registry::hkey_classes_rootclsid -include progid -recurse | foreach {$_.getvalue("")}
dao.dbengine.36
dao.privatedbengine.36
dao.tabledef.36
dao.field.36
dao.index.36
ps c:powershell> dir registry::hkey_classes_rootclsid -include progid -recurse
| foreach {$_.getvalue("")} | select -first 10
dao.dbengine.36
dao.privatedbengine.36
dao.tabledef.36
dao.field.36
dao.index.36
dao.group.36
dao.user.36
dao.querydef.36
dao.relation.36
file
......
怎样使用com对象
一旦得到了com对象的progid,就可以使用new-object创建com对象,只需要指定参数为-comobject。
ps c:powershell> new-object -comobject dao.relation.36
properties : system.__comobject
name :
table :
foreigntable :
attributes : 0
fields : system.__comobject
partialreplica :
com对象的和.net对象相似,任然可是使用get-member 得到该对象的所有熟悉和方法:
ps c:powershell> $dbeng=new-object -comobject dao.privatedbengine.36
ps c:powershell> $dbeng | get-member -me *method
typename: system.__comobject#{00000021-0000-0010-8000-00aa006d2ea4}
name membertype definition
---- ---------- ----------
begintrans method void begintrans ()
committrans method void committrans (int)
compactdatabase method void compactdatabase (string, string, variant...
createdatabase method database createdatabase (string, string, vari...
createworkspace method workspace createworkspace (string, string, st...
freelocks method void freelocks ()
idle method void idle (variant)
isamstats method int isamstats (int, variant)
openconnection method connection openconnection (string, variant, v...
opendatabase method database opendatabase (string, variant, varia...
registerdatabase method void registerdatabase (string, string, bool, ...
repairdatabase method void repairdatabase (string)
rollback method void rollback ()
setdataaccessoption method void setdataaccessoption (short, variant)
setdefaultworkspace method void setdefaultworkspace (string, string)
setoption method void setoption (int, variant)
_30_createworkspace method workspace _30_createworkspace (string, string...
ps c:powershell> $dbeng | get-member -me *property
typename: system.__comobject#{00000021-0000-0010-8000-00aa006d2ea4}
name membertype definition
---- ---------- ----------
defaultpassword property string defaultpassword () {set}
defaulttype property int defaulttype () {get} {set}
defaultuser property string defaultuser () {set}
errors property errors errors () {get}
inipath property string inipath () {get} {set}
logintimeout property short logintimeout () {get} {set}
properties property properties properties () {get}
systemdb property string systemdb () {get} {set}
version property string version () {get}
workspaces property workspaces workspaces () {get}
常用的com对象中有
wscript.shell,
wscript.network,
scripting.filesystemobject,
internetexplorer.application,
word.application,
shell.application
下面的例子使用wscript.shell com对象和它的方法createshortcut()做桌面上创建一个powershell快捷方式:
ps c:powershell> $wshell=new-object -comobject wscript.shell
ps c:powershell> $path=[environment]::getfolderpath('desktop')
ps c:powershell> $link=$wshell.createshortcut("$pathpowershell.lnk")
ps c:powershell> $link | get-member
typename: system.__comobject#{f935dc23-1cf0-11d0-adb9-00c04fd58a0b}
name membertype definition
---- ---------- ----------
load method void load (string)
save method void save ()
arguments property string arguments () {get} {set}
description property string description () {get} {set}
fullname property string fullname () {get}
hotkey property string hotkey () {get} {set}
iconlocation property string iconlocation () {get} {set}
relativepath property string relativepath () {set}
targetpath property string targetpath () {get} {set}
windowstyle property int windowstyle () {get} {set}
workingdirectory property string workingdirectory () {get} {set}
ps c:powershell> $link.targetpath='powershell.exe'
ps c:powershell> $link.description="启动powershell"
ps c:powershell> $link.workingdirectory=$profile
ps c:powershell> $link.iconlocation='powershell.exe'
ps c:powershell> $link.save()