一般來說,如何找到有關 PowerShell 物件的更多資訊?

一般來說,如何找到有關 PowerShell 物件的更多資訊?

我在研究如何查看電腦 Windows 更新檢查的狀態時發現了以下程式碼:

$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
$Updates = @($UpdateSearcher.Search("IsHidden=0 and IsInstalled=0").Updates)
$Updates | Select-Object Title

這段程式碼並不能滿足我的需求,但我覺得它可能夠強大。我擺脫了Select-Object Title限制,返回的有很多屬性,即Type激起了我的興趣,因為我希望這Type可以區分驅動程式更新、第三方更新(如 Microsoft Silverlight)和真正的“Windows 更新”,但無論如何我努力尋找更多信息,但找不到任何東西。

我在 google 和 MSDN 上搜尋過"Microsoft.Updates.Session",我找不到任何真正告訴我它可用的屬性以及枚舉含義的來源(例如 Type=1 與 Type=2)。

是否有我應該搜尋的 PowerShell 物件參考,或者當我需要時如何查找有關 PowerShell 物件的更多資訊?

答案1

首先,該類型[Microsoft.Update.Session]實際上並不是內建的 Powershell 對象,而是Windows 更新代理程式 (WUA) API。因此,它沒有任何內建的說明文件或 powershell 可以向您展示的範例,但可以在 Microsoft 網站上搜尋。

連結的 MS 文件有一些很好的範例,說明如何使用 api 透過 Windows 更新執行不同的操作,並且大多數可以直接轉換為在 Powershell 中使用。

我之前碰巧用過這個,所以這裡有一些 powershell 的基礎知識:

# Create a new update session, and search for updates
$updateObject = New-Object -ComObject Microsoft.Update.Session
$updateObject.ClientApplicationID = "Example Client ID"
$updateSearcher = $updateObject.CreateUpdateSearcher()

# Search for updates using a simple search filter, and save the results to a variable:
$searchResults = $updateSearcher.Search("IsInstalled=0")

# If there are updates available, download them:
if ($searchResults.Updates -and $searchResults.Updates.count -gt 0){
    $count=$searchResults.Updates.count
    Write-Output ( "Found " + $searchResults.Updates.Count + " Updates" )
    $updateDownloader = $updateObject.CreateUpdateDownloader()
    $updateDownloader.Updates = $searchResults.Updates
    Write-Output "Downloading..."
    $updateDownloader.Download()

    # Then install the updates:
    $updateInstaller = $updateObject.CreateUpdateInstaller()
    $updateInstaller.Updates = $searchResults.Updates
    Write-Output "Installing..."
    $result = $updateInstaller.Install()

    # Then output the result
    Write-Output ("Installation Result: " + $Result.ResultCode)
    Write-Output ("Reboot Required: " + $Result.RebootRequired)
}
else { Write-Output "No updates found. Exiting..." }

至於搜尋特定更新,您需要將過濾器新增至$UpdateSearcher.搜尋()方法。例如,看起來可以有 type='Software' 或 type='Driver' 。

請注意,WUA API 有一個錯誤/功能,通常需要它在電腦上本地運行而不是遠端啟動,儘管您可以透過建立運行腳本的計劃任務來解決這個問題。

最後,實際回答您的問題 - 作為一般規則,Get-Member $MyObject它將Get-Help $MyCommand幫助您發現大多數內建的 Powershell 功能。

答案2

最簡單的方法是使用Get-Membercmdlet,例如:

$Updates | Get-Member

在這種情況下,它很可能是 COM 對象,但它仍然應該向您顯示方法和屬性

相關內容