顯示 PowerShell 物件的所有屬性

Migel Hewage Nimesha 2024年2月15日
  1. 使用 Get-WmiObject 顯示 PowerShell 物件的所有屬性
  2. 檢索類例項/物件資訊
  3. 使用 Format-List Cmdlet 顯示屬性
顯示 PowerShell 物件的所有屬性

需要查詢有關 Windows 機器及其元件(如網路、應用程式和服務)的資訊。公共資訊模型 (CIM) 是用於操作這些元件及其資訊的開源標準。

Microsoft 已在 CIM 之上實施其稱為 Windows Management Instrumentation (WMI) 的標準,以查詢和操作計算機、服務和網路資訊。

使用 Get-WmiObject 顯示 PowerShell 物件的所有屬性

Get-WmiObject cmdlet 可用於從 WMI 儲存庫請求資訊。此外,它還使你能夠使用遠端系統。

因此,Windows 系統管理過程變得容易得多。讓我們使用 Get-WmiObject cmdlet 檢查可用的類。

Get-WmiObject -List

此命令將輸出本地計算機預設名稱空間中可用的所有類。Windows 計算機中的預設名稱空間是 ROOT\cimv2

輸出:

Get-WmiObject

可以指定與預設名稱空間不同的名稱空間 (ROOT\cimv2)。我們可以使用 -Namespace 引數來更改預設名稱空間。

Get-WmiObject -Namespace <custom_namespace>

檢索類例項/物件資訊

有數百個 WMI 類可供使用。我們可以檢索給定類的資訊,如下所示。

Get-WmiObject -Class Win32_computersystem

在這裡,我們正在檢索 Win32_computersystem 類的資訊。它返回 Win32_computersystem 物件及其屬性,如下所示。

Get-WmiObject -Class

輸出中顯示的屬性數量有限。Win32_computersystem 物件包含比這些更多的屬性。

我們可以使用 Format-List cmdlet 來顯示檢索到的物件的所有屬性。

使用 Format-List Cmdlet 顯示屬性

Format-List 可以與另一個命令的輸出一起通過管道傳輸以格式化結果。此 cmdlet 可以顯示檢索到的 WMI 物件的指定或所有屬性。

讓我們為 Win32_computersystem 例項指定一些屬性。

Get-WmiObject -Class Win32_computersystem | Format-List -Property Name, Model, Manufacturer

輸出:

格式列表

Get-WmiObject 返回 Win32_computersystem 物件的預設屬性。然後輸出將通過管道運算子(|)傳遞給 Format-List 命令。

我們可以將 -Property 引數傳遞給 Format-List cmdlet。這將過濾掉具有給定三個屬性的最終輸出。

有時,檢查指定 WMI 物件的所有可用屬性和值很重要。在這種情況下,你需要使用 -Property * 來檢索所有可用的屬性及其值。

* 表示所有屬性。

Get-WmiObject -Class Win32_computersystem | Format-List -Property *

輸出:

格式列表-屬性

輸出包含可用於 Win32_computersystem 物件的所有屬性和值。我們可以省略上面命令中的 -Property 引數並編寫它。

Get-WmiObject -Class Win32_computersystem | Format-List *

上面的命令應該顯示與上面相同的輸出。

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

相關文章 - PowerShell Object