. The methods in the net type are powerful. You can create a new object from a type's constructor, or you can convert an existing object to a specified type.
Create a new object from New-object
If you use a constructor to create an instance object of a specified type, the type must contain at least one constructor that matches the signature. For example, you can create a string that contains a specified number of characters by character and number:
PS c:powershell> new-object String (' * ', 100)
*******************************************************************************
*********************
Why the above method is supported because the string class contains a void. ctor (Char, Int32) constructor
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)
Creating objects with type conversions
New-object can be replaced by type conversion
PS c:powershell> $date = "1999-9-1 10:23:44" PS c:powershell> $date. GetType (). Fullnamesystem.stringps c:powershell> $date 1999-9-1 10:23:44ps c:powershell> [DateTime] $date = " 1999-9-1 10:23:44 "PS c:powershell> $date. GetType (). Fullnamesystem.datetimeps c:powershell> $date September 1, 1999 10:23:44
You can also convert objects directly to an array if conditions permit
PS c:powershell> [char[]] "mossfly.com" Mossfly.comps c:powershell> [int[]][char[]] "mossfly.com" 1091111151151021081214699111109
Loading assemblies
Customize a simple C # class library to compile to 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.) {this . name = name; This. Age = Age; } public override string ToString () { return string. Format ("name={0}; Age={1} ", this. Name,this. age);}}}
Load the DLL in PowerShell and use the constructor of the student class in it to generate an instance, and finally call the ToString () method.
PS c:powershell> ls. Test.dll directory: C:powershellmode lastwritetime Length Name---- ------------- -----------A--- 2012/1/13 10:49 4608 test.dllps c:powershell> $TestDLL =ls. Test.dllps c:powershell> [Reflection.assembly]::loadfile ($TestDLL. FullName) GAC Version location --- ------- --------False v2.0.50727 c:powershelltest.dllps c:powershell> $stu =new-object Test.student (' Mosser ') PS c:powershell> $stuName age ---- ---Mosser 22PS c:powershell> $stu. ToString () Name=mosser; Age=22
Using COM objects
As. NET, PowerShell can load and access COM objects.
To view available COM objects
Each COM object has a unique identifier that is stored in the registry and wants to traverse through the available COM objects, but accesses the registry directly.
Dir registry::hkey_classes_rootclsid -include progid-recurse | foreach {$_. GetValue ("")}dao. Dbengine.36dao.privatedbengine.36dao.tabledef.36dao.field.36dao.index.36ps c:powershell> Dir REGISTRY::HKEY_ Classes_rootclsid-include progid-recurse| foreach {$_. GetValue ("")} | Select-first 10DAO. Dbengine.36dao.privatedbengine.36dao.tabledef.36dao.field.36dao.index.36dao.group.36dao.user.36dao.querydef.36dao.relatio N.36file ...
How to use COM objects
Once you have the ProgID of the COM object, you can use New-object to create the COM object, just specify the parameter as-comobject.
PS c:powershell> new-object-comobject DAO. Relation.36properties : system.__comobjectname: Table : foreigntable : Attributes : 0Fields : System.__comobjectpartialreplica:
COM objects are similar to. NET objects, but use Get-member to get all the familiarity and methods of the object:
PS c:powershell> $DBEng =new-object-comobject DAO. Privatedbengine.36ps c:powershell> $DBEng | Get-member-me *method Typename:system.__comobject#{00000021-0000-0010-8000-00aa006d2ea4}name MemberType Definition------------------------BeginTrans Method void BeginTrans () CommitTrans M ethod 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 Meth OD 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 () setdataaccessopt Ion method void Setdataaccessoption (short, Variant) Setdefaultworkspace method void Setdefaultworkspace (string, S Tring) SetOption method void SetOption (int, Variant) _30_createworkspace method Workspace _30_createworks Pace (String, string ... PS c:powershell> $DBEng | Get-member-me *property typename:system.__comobject#{00000021-0000-0010-8000-00aa006d2ea4}name MemberType D efinition------------------------DefaultPassword Property String DefaultPassword () {Set}defaulttype Pr Operty int DefaultType () {get} {Set}defaultuser property string DefaultUser () {Set}errors Property Er Rors Errors () {Get}inipath property string IniPath () {get} {Set}logintimeout Property short LoginTimeout () {get} {Set}properties Property Properties Properties () {Get}systemdbProperty String SystemDB () {get} {Set}version property string Version () {get}workspaces Spaces Workspaces () {get}
There are wscript.shell in common COM objects,
Wscript.Network,
Scripting.FileSystemObject,
Internetexplorer.application,
Word.Application,
Shell.Application
The following example uses the Wscript.Shell COM object and its method CreateShortcut () to create a PowerShell shortcut on the desktop:
PS c:powershell> $wshell =new-object-comobject wscript.shellps 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 () Ar Guments Property String Arguments () {get} {Set}description property string Description () {get} {Set}full Name Property String FullName () {Get}hotkey property string Hotkey () {get} {set}iconlocation P Roperty string iconlocation () {get} {Set}relativepath property string RelativePath () {Set}targetpath prope Rty 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= "Start Powershell" PS c:powershell> $link. workingdirectory= $PROFILEPS c:powershell> $link. iconlocation= ' Powershell.exe ' PS c:powershell> $link. Save ()
PowerShell Creation Object