[英]Script for exporting all installed win updates on multiple servers
該腳本在單台服務器上運行良好,但如何在多台服務器上運行呢?
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
$Updates = $Searcher.QueryHistory(0,$HistoryCount)
$Updates | Select Title,@{l='Name';e={$($_.Categories).Name}},Date
要堅持您的原始問題,您可以使用Invoke-Command
獲得多個服務器的結果,如下所示:
# set the credentials for admin access on the servers
$cred = Get-Credential 'Please enter your admin credentials'
# create an array of the servers you need to probe
$servers = 'Server01', 'Server02'
$result = Invoke-Command -ComputerName $servers -Credential $cred -ScriptBlock {
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
$Searcher.QueryHistory(0,$HistoryCount) |
Select-Object @{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME}}, Title,
@{Name = 'Name';Expression = {$($_.Categories).Name}}, Date
# make sure you destroy the used COM objects from memory when done
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Searcher)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Session)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# remove the extra properties PowerShell added
$result = $result | Select-Object * -ExcludeProperty PS*, RunspaceId
# show on screen
$result | Format-List
# output to csv file
$result | Export-Csv -Path 'X:\Somewhere\updates.csv' -NoTypeInformation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.