Powershell:在多台服务器上修补 Windows

Powershell:在多台服务器上修补 Windows

我们有大约 1000 多台服务器,它们通过 WSUS 进行修补。我们在 WSUS 中分配了补丁,然后登录服务器并下载并安装补丁。完成后,我们按照计划重新启动机器。

我想自动下载补丁并安装补丁。我有一些脚本,但不确定这是不是最好的方法,或者还有其他方法可用。

以下是脚本

$UpdateSession = New-Object -Com Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()

Write-Host("Searching for applicable updates...") -Fore Green

$SearchResult = $UpdateSearcher.Search("IsInstalled=0 and Type='Software'")

Write-Host("")
Write-Host("List of applicable items on the machine:") -Fore Green
For ($X = 0; $X -lt $SearchResult.Updates.Count; $X++){
    $Update = $SearchResult.Updates.Item($X)
    Write-Host( ($X + 1).ToString() + "> " + $Update.Title)
}

If ($SearchResult.Updates.Count -eq 0) {
    Write-Host("There are no applicable updates.")
    Exit
}

#Write-Host("")
#Write-Host("Creating collection of updates to download:") -Fore Green

$UpdatesToDownload = New-Object -Com Microsoft.Update.UpdateColl

For ($X = 0; $X -lt $SearchResult.Updates.Count; $X++){
    $Update = $SearchResult.Updates.Item($X)
    #Write-Host( ($X + 1).ToString() + "> Adding: " + $Update.Title)
    $Null = $UpdatesToDownload.Add($Update)
}

Write-Host("")
Write-Host("Downloading Updates...")  -Fore Green

$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
$Null = $Downloader.Download()

#Write-Host("")
#Write-Host("List of Downloaded Updates...") -Fore Green

$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl

For ($X = 0; $X -lt $SearchResult.Updates.Count; $X++){
    $Update = $SearchResult.Updates.Item($X)
    If ($Update.IsDownloaded) {
        #Write-Host( ($X + 1).ToString() + "> " + $Update.Title)
        $Null = $UpdatesToInstall.Add($Update)        
    }
}

$Install = [System.String]$Args[0]
$Reboot  = [System.String]$Args[1]

If (!$Install){
    $Install = Read-Host("Would you like to install these updates now? (Y/N)")
}

If ($Install.ToUpper() -eq "Y" -or $Install.ToUpper() -eq "YES"){
    Write-Host("")
    Write-Host("Installing Updates...") -Fore Green

    $Installer = $UpdateSession.CreateUpdateInstaller()
    $Installer.Updates = $UpdatesToInstall

    $InstallationResult = $Installer.Install()

    Write-Host("")
    Write-Host("List of Updates Installed with Results:") -Fore Green

    For ($X = 0; $X -lt $UpdatesToInstall.Count; $X++){
        Write-Host($UpdatesToInstall.Item($X).Title + ": " + $InstallationResult.GetUpdateResult($X).ResultCode)
    }

    Write-Host("")
    Write-Host("Installation Result: " + $InstallationResult.ResultCode)
    Write-Host("    Reboot Required: " + $InstallationResult.RebootRequired)

    If ($InstallationResult.RebootRequire -eq $True){
        If (!$Reboot){
            $Reboot = Read-Host("Would you like to install these updates now? (Y/N)")
        }

        If ($Reboot.ToUpper() -eq "Y" -or $Reboot.ToUpper() -eq "YES"){
            Write-Host("")
            Write-Host("Rebooting...") -Fore Green
            (Get-WMIObject -Class Win32_OperatingSystem).Reboot()
        }
    }
}

此脚本是否能够在多台服务器上执行活动?我们可以获取有关下载成功/安装成功的报告吗?

还有其他选项可以执行相同的活动吗?

谢谢你的时间。

相关内容