如何使用 PowerShell Expand 屬性選擇對象

PowerShell 的 Select-Object 命令讓您能夠從一組輸入的物件中無縫地挑選物件和屬性。但有時您可能需要更多資訊。ExpandProperty 是 Select-Object 的一個開關,可以展開有關特定屬性的詳細資訊。

在這個以範例為主的教學中,了解 PowerShell 的 ExpandProperty (-ExpandProperty) 開關的強大功能。

先決條件

本教學將進行實作演示。如果您想跟著做,您只需要一台安裝了 PowerShell 5.1 或更新版本的 Windows 電腦。本文使用的是安裝了 PowerShell 5.1 的 Windows 10 電腦。

使用 PowerShell 的 ExpandProperty 提取屬性物件值

登入您的 Windows 電腦並啟動 PowerShell 終端機。在這一部分中,您將熟悉 Select-Object 命令及其 ExpandProperty 開關。

1. 執行 Get-Service 命令以獲取系統上所有服務的清單。將輸出導向 Select-Object 命令,以只選擇並顯示每個服務物件的名稱屬性,如下所示。

Get-Service | Select-Object -Property Name

現在,您應該會看到一列物件,代表著系統上服務的名稱清單,如下所示(截圖為縮小顯示)。

Selecting the names of the existing system services

2. 在上一個管道的末尾運行 Get-Member cmdlet 以確認 select-object 返回的物件類型如下。知道物件的類型有助於撰寫函數或 cmdlet 來擴展您的管道。

Get-Service | Select-Object -Property Name | Get-Member

請參考下面的截圖,您將注意到儘管 -Property 開關輸出的似乎是一個字符串列表,但那些是 Selected 列表中的System.ServiceProcess.ServiceController 物件。

Viewing the type of a ServiceController object with Get-Member

3. 將 Get-Members cmdlet 替換為 mkdir 以自動為每個服務創建一個文件夾。 -first 5 開關限制返回的物件數量。然後,mkdir 函數將僅根據管道中的物件創建五個文件夾。

Get-Service | Select-Object -Property Name -First 5 | mkdir

您應該看到類似下面截圖的輸出,指示成功嘗試。

Creating a folder for the services on your system

4. 通過將 mkdir 替換為 rmdir 來刪除文件夾,如下片段所示。在您的終端上打開 -Verbose 開關以顯示詳細輸出。

Get-Service | Select-Object -Property Name -First 5 | rmdir -Verbose

rmdir cmdlet 預期字串,無法處理 ServiceController 物件的 hashtable 表示。您的嘗試應該會因為幾個錯誤而失敗,如下圖所示。

Demonstrating that some cmdlets expect string values instead of hashtables

5. 將 -Property 開關更改為 -ExpandProperty 開關,如下面的程式片段所示。這樣做可以在您只需要字符串時將屬性的值作為字符串獲取。

Get-Service | Select-Object -ExpandProperty Name -First 5 | rmdir -Verbose

與 rmdir 結合,您應該會看到自動刪除文件夾的嘗試成功而沒有錯誤,如下圖所示。

Deleting folders named after the services expanded

創建一組具有特定屬性的選定對象

為了縮短部分中的代碼片段,請按照以下方式在您的機器上為前 20 個服務創建一個變量。

運行 Get-Service 命令,通過 Select-Object cmdlet 選擇前 20 個對象,並將對象存儲在名為 $testServices 的變量中。如下所示僅選擇 NameStatusDependentServices 屬性。

$testServices = Get-Service | Select-Object -Property Name,Status,DependentServices -First 20

PowerShell 展開屬性開關一次只能展開一個屬性,而不像屬性開關可以同時選擇多個屬性。

執行變數名稱為$testServices的程式碼,以查看其內容結構。

$testServices

您應該會看到一個顯示您列出的服務屬性的表格,類似下面的截圖。

Displaying the contents of $testServices

使用 PowerShell 擴展屬性展開集合屬性

A PowerShell object can have properties that are collections of objects, also known as collection properties. In this section, you will learn how to use PowerShell Expand Property to expand a collection property to view its members’ properties.

$testServices變數執行Select-Object cmdlet,並展開每個服務的DependentServices屬性。

$testServices | Select-Object -ExpandProperty DependentServices

由於DependentServices是一個集合屬性,輸出將是所有已填充集合中的服務列表,如下所示。請注意,由於合併了來自擴展的DependentServices的結果,列表中存在重複項。

Expanding the DependentServices property

使用 PowerShell 擴展屬性組合屬性以便進行分類輸出

現在您擁有了一個依賴服務的列表,但您如何知道它們依賴於哪個服務呢?在本節中,您將學習如何使用-Property開關來對展開的屬性進行分類。

運行以下命令以按NameDependentServices屬性進行分類。

$testServices | Select-Object -ExpandProperty DependentServices -Property Name

由於依賴服務已經有了一個Name屬性,分類步驟將失敗,如下所示。

以下錯誤告訴您,服務的初始列表(在$testServices中)已經有一個與擴展的DependentServices屬性中的Name屬性衝突的Name屬性。

Attempting to group an expanded property by the parent object name

一種解決方法是創建Calculated Properties來將識別屬性重命名如下。

保存先前的命令輸出,修改為將名稱屬性指定為名為$depServ的變量中的計算屬性Depends On。Depends on將包含$testService中每個服務的Name值。

在本教程中,屬性名Depends on是任意選擇的,可以是您選擇的任何表達式。

$depServ = $testServices | Select-Object -ExpandProperty DependentServices -Property @{name="Depends On"; expr={$_.Name}}

通過$depServ將對象傳遞給Select-Object,以選擇依賴服務的NameStatusDepends On屬性。

$depServ | Select-Object -Property Name,Status,"Depends On"

現在,您可以查看哪些服務依賴於$testServices中的服務,如下所示。

Grouping an expanded property using a calculated property

將Format-List與PowerShell Expand Property結合以提高清晰度

PowerShell可能會限制在表格輸出中顯示的對象屬性,-ExpandProperty開關輸出可能會遇到相同的問題。您可以將擴展屬性顯示為列表,而不是作為解決方法。

運行下面的Get-Process cmdlet以檢索explorer進程使用的所有modules。啟用-ExpandProperty開關以顯示模塊的屬性。

Get-Process explorer | select-object -ExpandProperty Modules

在屏幕上顯示的輸出應該類似於以下屏幕截圖,自動截斷以僅顯示三個屬性。

Viewing the truncated output of an expanded property

將對象管道傳遞到 Format-List cmdlet,以查看模組對象的所有值,以更方便的列表形式呈現。

Get-Process explorer | select-object -ExpandProperty Modules | Format-List

如下所示,輸出列舉了所有屬性及其值。您可以進一步將數據保存到文件中,或將其傳遞給另一個 cmdlet 進行進一步處理。

Converting the output of an expanded property to a list with Format-List

結論

通過成功達到這一步,您已經看到如何從 PowerShell 的 Select-Object cmdlet 中受益於 PowerShell Expand Property 開關 (-ExpandProperty)。PowerShell 提供了許多常見任務的實用 cmdlet。您可以通過學習如何使用 PowerShell Cat 快速顯示文件,來擴展您的新知識。

Source:
https://adamtheautomator.com/powershell-expand-property/