檢視對象結構
因為物件在PowerShell中扮演如此集中的角色,因此有數個原生命令設計來使用任意物件類型。 最重要的一個是 Get-Member
命令。
分析命令傳回之物件的最簡單技巧,就是將該命令的輸出傳送至 Get-Member
Cmdlet。 Cmdlet Get-Member
會顯示物件類型的正式名稱,以及其成員的完整清單。 傳回的項目數目有時可能很壓倒性。 例如,進程物件可以有超過100個成員。
下列命令可讓您查看 Process 物件的所有成員,並逐頁查看輸出。
Get-Process | Get-Member | Out-Host -Paging
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
NPM AliasProperty NPM = NonpagedSystemMemorySize
PM AliasProperty PM = PagedMemorySize
VM AliasProperty VM = VirtualMemorySize
WS AliasProperty WS = WorkingSet
add_Disposed Method System.Void add_Disposed(Event...
...
我們可以藉由篩選我們想要查看的元素,讓這份完整資訊清單更容易使用。 命令 Get-Member
可讓您只列出屬於屬性的成員。 有數種形式的屬性。 Cmdlet 會使用 MemberType 參數搭配值 Properties
來顯示型別的屬性。 產生的清單仍然很長,但更容易管理:
Get-Process | Get-Member -MemberType Properties
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
...
ExitCode Property System.Int32 ExitCode {get;}
...
Handle Property System.IntPtr Handle {get;}
...
CPU ScriptProperty System.Object CPU {get=$this.Total...
...
Path ScriptProperty System.Object Path {get=$this.Main...
...
注意
MemberType 的允許值為 AliasProperty、CodeProperty、Property、NoteProperty、ScriptProperty、Properties、PropertySet、Method、CodeMethod、ScriptMethod、Methods、ParameterizedProperty、MemberSet 和 All。
進程有超過 60 個屬性。 根據預設,PowerShell 會決定如何使用儲存在 XML 檔案中以 結尾 .format.ps1xml
之名稱的資訊來顯示物件類型。 進程物件的格式化定義會儲存在 中 DotNetTypes.format.ps1xml
。
如果您需要查看 PowerShell 預設顯示的屬性,您可以使用 Cmdlet 來格式化輸出 Format-*
。