建立 Microsoft .NET Framework 或 COM 物件的執行個體。

語法

New-Object -ComObject <string> [-Strict] [-Property <hashtable>] [<CommonParameters>]

New-Object [-TypeName] <string> [[-ArgumentList] <Object[]>] [-Property <hashtable>] [<CommonParameters>]

描述

New-Object Cmdlet 會建立 .NET Framework 或 COM 物件的執行個體。

您可以指定 .NET Framework 類別的型別或 COM 物件的 ProgID。根據預設,若輸入 .NET Framework 類別的完整名稱,此 Cmdlet 會傳回該類別執行個體的參考。若要建立 COM 物件的執行個體,請使用 ComObject 參數,並指定物件的 ProgID 當作其值。

參數

-ArgumentList <Object[]>

指定要傳遞給 .NET Framework 類別之建構函式的引數清單。使用逗號 (,) 分隔清單中的元素。ArgumentList 的別名為 Args。

必要?

false

位置?

2

預設值

接受管線輸入?

false

接受萬用字元?

false

-ComObject <string>

指定 COM 物件的程式設計識別碼 (ProgID)。

必要?

true

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Property <hashtable>

設定屬性值,並呼叫新物件的方法。

請輸入一個符合下列條件的雜湊表:索引鍵是屬性或方法的名稱,而值是屬性值或方法引數。New-Object 會建立物件並設定每一個屬性值,然後依照方法出現在雜湊表的順序來呼叫每一個方法。

如果新的物件衍生自 PSObject 類別,而且您在此物件上指定不存在的屬性,New-Object 會將指定的屬性當做 NoteProperty 加入至此物件。如果此物件不是 PSObject,此命令會產生非終止錯誤。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Strict

指定一旦試圖建立的 COM 物件使用 Interop 組件時,應引發的錯誤。如此即可分辨實際的 COM 物件以及具有可呼叫 COM 包裝函式的 .NET Framework 物件。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-TypeName <string>

指定 .NET Framework 類別的完整名稱。不可同時指定 TypeName 參數和 ComObject 參數。

必要?

true

位置?

1

預設值

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

您無法經由管道將輸入輸出至這個 Cmdlet。

輸出

Object

New-Object 會傳回建立的物件。

附註

New-Object 會提供 VBScript CreateObject 函數的最常用功能。VBScript 中的陳述式如 Set objShell = CreateObject("Shell.Application"),可以在 Windows PowerShell 中轉譯為 $objShell = new-object -comobject "Shell.Application"。

New-Object 讓您能從命令列及指令碼中輕鬆使用 .NET Framework 物件,擴展在 Windows Script Host 環境下可使用的功能。

範例 1

C:\PS>new-object -typename System.Version -argumentlist "1.2.3.4" 

Major  Minor  Build  Revision
-----  -----  -----  --------
1      2      3      4

描述
-----------
這個命令使用字串 "1.2.3.4" 當做建構函式,以建立 System.Version 物件。






範例 2

C:\PS>$ie = new-object -comobject InternetExplorer.Application -property @{navigate2="www.microsoft.com"; visible = $true}

描述
-----------
這個命令建立代表 Internet Explorer 應用程式的 COM 物件執行個體。它會使用 Property 參數,以便呼叫 Navigate2 方法,並將物件的 Visible 屬性設為 $true,讓應用程式顯示出來。

此命令相當於以下命令:

$ie = new-object -comobject InternetExplorer.Application
$ie.navigate2("www.microsoft.com")
$ie.visible = $true






範例 3

C:\PS>$a=new-object -comobject Word.Application -strict -property @{visible=$true}

New-Object : The object written to the pipeline is an instance of the type
"Microsoft.Office.Interop.Word.ApplicationClass" from the component's prima
ry interop assembly. If this type exposes different members than the IDispa
tch members, scripts written to work with this object might not work if the
 primary interop assembly is not installed.
At line:1 char:14
+ $a=New-Object  <<<< -COM Word.Application -Strict; $a.visible=$true

描述
-----------
這個命令示範,指定 Strict 參數將會導致 New-Object Cmdlet 在建立的 COM 物件使用 Interop 組件時,產生非終止錯誤。






範例 4

C:\PS>$objshell = new-object -comobject "Shell.Application"

C:\PS> $objshell | get-member

C:\PS> $objshell.ToggleDesktop()

描述
-----------
這個命令會使用 ComObject 參數建立 ProgID 為 "Shell.Application" 的 COM 物件,然後將產生的物件儲存到 $objShell 變數。

第二個命令會經由管道將 $objShell 變數輸出至 Get-Member Cmdlet,讓後者顯示 COM 物件的屬性和方法。

第三個命令會呼叫該物件的 ToggleDesktop 方法,將您的桌面上開啟的視窗最小化。






請參閱




目錄