繁体   English   中英

用于在多台服务器上导出所有已安装的 win 更新的脚本

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM