主頁 > 知識庫 > Windows Powershell創(chuàng)建對象

Windows Powershell創(chuàng)建對象

熱門標簽:萬全縣地圖標注app 新鄭電銷機器人一個月多少錢 地圖標注的圖案 莫拉克電梯系統(tǒng)外呼怎么設置 電話機器人公司招聘 印臺區(qū)呼叫中心外呼系統(tǒng) 六寸地圖標注點怎么刪除 騰訊地圖標注中心怎么標注 如何根據(jù)經緯度百度地圖標注

通過New-Object創(chuàng)建新對象

如果使用構造函數(shù)創(chuàng)建一個指定類型的實例對象,該類型必須至少包含一個簽名相匹配的構造函數(shù)。例如可以通過字符和數(shù)字創(chuàng)建一個包含指定個數(shù)字符的字符串:

復制代碼 代碼如下:

PS C:Powershell> New-Object String(‘*',100)

*******************************************************************************
*********************

為什么支持上面的方法,原因是String類中包含一個Void .ctor(Char, Int32) 構造函數(shù)

復制代碼 代碼如下:

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)

通過類型轉換創(chuàng)建對象

通過類型轉換可以替代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

如果條件允許,也可以直接將對象轉換成數(shù)組

復制代碼 代碼如下:

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類的構造函數(shù)生成一個實例,最后調用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創(chuàng)建COM對象,只需要指定參數(shù)為-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()做桌面上創(chuàng)建一個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()

您可能感興趣的文章:
  • Windows Powershell條件表達式之條件操作符
  • Windows Powershell Where-Object 條件過濾

標簽:疫苗接種 汕頭 湘潭 南昌 襄陽 臨汾 天水 喀什

巨人網(wǎng)絡通訊聲明:本文標題《Windows Powershell創(chuàng)建對象》,本文關鍵詞  Windows,Powershell,創(chuàng)建,對象,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Windows Powershell創(chuàng)建對象》相關的同類信息!
  • 本頁收集關于Windows Powershell創(chuàng)建對象的相關信息資訊供網(wǎng)民參考!
  • 推薦文章