Thread: Общие вопросы (General Questions)/Windows Searching, Downloading, and Installing Updates

Windows Searching, Downloading, and Installing Updates
Searching, Downloading, and Installing Updates

Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"

Set updateSearcher = updateSession.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
    WScript.Echo "There are no applicable updates."
    WScript.Quit
End If

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    addThisUpdate = false
    If update.InstallationBehavior.CanRequestUserInput = true Then
        WScript.Echo I + 1 & "> skipping: " & update.Title & _
        " because it requires user input"
    Else
        If update.EulaAccepted = false Then
            WScript.Echo I + 1 & "> note: " & update.Title & _
            " has a license agreement that must be accepted:"
            WScript.Echo update.EulaText
            WScript.Echo "Do you accept this license agreement? (Y/N)"
            strInput = WScript.StdIn.Readline
            WScript.Echo
            If (strInput = "Y" or strInput = "y") Then
                update.AcceptEula()
                addThisUpdate = true
            Else
                WScript.Echo I + 1 & "> skipping: " & update.Title & _
                " because the license agreement was declined"
            End If
        Else
            addThisUpdate = true
        End If
    End If
    If addThisUpdate = true Then
        WScript.Echo I + 1 & "> adding: " & update.Title
        updatesToDownload.Add(update)
    End If
Next

If updatesToDownload.Count = 0 Then
    WScript.Echo "All applicable updates were skipped."
    WScript.Quit
End If
   
WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
        WScript.Echo I + 1 & "> " & update.Title
        updatesToInstall.Add(update)   
        If update.InstallationBehavior.RebootBehavior > 0 Then
            rebootMayBeRequired = true
        End If
    End If
Next

If updatesToInstall.Count = 0 Then
    WScript.Echo "No updates were successfully downloaded."
    WScript.Quit
End If

If rebootMayBeRequired = true Then
    WScript.Echo vbCRLF & "These updates may require a reboot."
End If

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo

If (strInput = "Y" or strInput = "y") Then
    WScript.Echo "Installing updates..."
    Set installer = updateSession.CreateUpdateInstaller()
    installer.Updates = updatesToInstall
    Set installationResult = installer.Install()
   
    'Output results of install
    WScript.Echo "Installation Result: " & _
    installationResult.ResultCode
    WScript.Echo "Reboot Required: " & _
    installationResult.RebootRequired & vbCRLF
    WScript.Echo "Listing of updates installed " & _
    "and individual installation results:"
   
    For I = 0 to updatesToInstall.Count - 1
        WScript.Echo I + 1 & "> " & _
        updatesToInstall.Item(i).Title & _
        ": " & installationResult.GetUpdateResult(i).ResultCode       
    Next
End If


' To run it: cscript WUA_SearchDownloadInstall.vbs



Re: Windows Searching, Downloading, and Installing Updates
<#
    .SYNOPSIS
        Get and optionally install Windows Updates
    .DESCRIPTION
        This script will get all available udpates for the computer it is run on.
        It will then optionally install those updates, provided they do not require
        user input.
       
        This script was based off the original vbs that appeared on the MSDN site.
        Please see the Related Links section for the URL.
       
        Without any parameters the script will return the title of each update that
        is currently available.
    .PARAMETER Install
        When present the script will download and install each update. If the EulaAccept
        param has not been passed, only updates that don't have a Eula will be applied.
    .PARAMETER EulaAccept
        When present will allow the script to download and install all updates that are
        currently available.
    .EXAMPLE
        .\Get-WindowsUpdates.ps1
       
        There are no applicable updates
       
        Description
        -----------
        This system is currently patched and up to date.
    .NOTES
        ScriptName : Get-WindowsUpdates.ps1
        Created By : jspatton
        Date Coded : 08/29/2012 13:06:31
        ScriptName is used to register events for this script
 
        ErrorCodes
            100 = Success
            101 = Error
            102 = Warning
            104 = Information
    .LINK
        https://code.google.com/p/mod-posh/wiki/Production/Get-WindowsUpdates.ps1
    .LINK
        http://msdn.microsoft.com/en-us/library/windows/desktop/aa387102(v=vs.85).aspx
#>
[CmdletBinding()]
Param
    (
    [switch]$Install,
    [switch]$EulaAccept
    )
Begin
    {
        $ScriptName = $MyInvocation.MyCommand.ToString()
        $ScriptPath = $MyInvocation.MyCommand.Path
        $Username = $env:USERDOMAIN + "\" + $env:USERNAME
 
        New-EventLog -Source $ScriptName -LogName 'Windows Powershell' -ErrorAction SilentlyContinue
 
        $Message = "Script: " + $ScriptPath + "`nScript User: " + $Username + "`nStarted: " + (Get-Date).toString()
        Write-EventLog -LogName 'Windows Powershell' -Source $ScriptName -EventID "104" -EntryType "Information" -Message $Message
 
        # Dotsource in the functions you need.

        $UpdateSession = New-Object -ComObject 'Microsoft.Update.Session'
        $UpdateSession.ClientApplicationID = 'MSDN PowerShell Sample'
        }
Process
    {
        $UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
        $SearchResult = $UpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
       
        if ($Install)
        {
            Write-Verbose 'Creating a collection of updates to download:'
            $UpdatesToDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'
            foreach ($Update in $SearchResult.Updates)
            {
                [bool]$addThisUpdate = $false
                if ($Update.InstallationBehavior.CanRequestUserInput)
                {
                    Write-Verbose "> Skipping: $($Update.Title) because it requires user input"
                    }
                else
                {
                    if (!($Update.EulaAccepted))
                    {
                        Write-Verbose "> Note: $($Update.Title) has a license agreement that must be accepted:"
                        if ($EulaAccept)
                        {
                            $Update.AcceptEula()
                            [bool]$addThisUpdate = $true
                            }
                        else
                        {
                            Write-Verbose "> Skipping: $($Update.Title) because the license agreement was declined"
                            }
                        }
                    else
                    {
                        [bool]$addThisUpdate = $true
                        }
                    }
                if ([bool]$addThisUpdate)
                {
                    Write-Verbose "Adding: $($Update.Title)"
                    $UpdatesToDownload.Add($Update) |Out-Null
                    }
                }
            if ($UpdatesToDownload.Count -eq 0)
            {
                Write-Verbose 'All applicable updates were skipped.'
                break
                }

            Write-Verbose 'Downloading updates...'
            $Downloader = $UpdateSession.CreateUpdateDownloader()
            $Downloader.Updates = $UpdatesToDownload
            $Downloader.Download()

            $UpdatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'

            [bool]$rebootMayBeRequired = $false
            Write-Verbose 'Successfully downloaded updates:'

            foreach ($Update in $SearchResult.Updates)
            {
                if ($Update.IsDownloaded)
                {
                    Write-Verbose "> $($Update.Title)"
                    $UpdatesToInstall.Add($Update) |Out-Null
                   
                    if ($Update.InstallationBehavior.RebootBehavior -gt 0)
                    {
                        [bool]$rebootMayBeRequired = $true
                        }
                    }
                }
            if ($UpdatesToInstall.Count -eq 0)
            {
                Write-Verbose 'No updates were succsesfully downloaded'
                }

            if ($rebootMayBeRequired)
            {
                Write-Verbose 'These updates may require a reboot'
                }

            Write-Verbose 'Installing updates...'
           
            $Installer = $UpdateSession.CreateUpdateInstaller()
            $Installer.Updates = $UpdatesToInstall
            $InstallationResult = $Installer.Install()
           
            Write-Verbose "Installation Result: $($InstallationResult.ResultCode)"
            Write-Verbose "Reboot Required: $($InstallationResult.RebootRequired)"
            Write-Verbose 'Listing of updates installed and individual installation results'
           
            for($i=0; $i -lt $UpdatesToInstall.Count; $i++)
            {
                New-Object -TypeName PSObject -Property @{
                    Title = $UpdatesToInstall.Item($i).Title
                    Result = $InstallationResult.GetUpdateResult($i).ResultCode
                    }
                }
            }
        else
        {
            if ($SearchResult.Updates.Count -ne 0)
            {
                $SearchResult.Updates |Select-Object -Property Title, Description, SupportUrl, UninstallationNotes, RebootRequired |Format-List
                }
            else
            {
                Write-Host 'There are no applicable updates'
                }
            }
        }
End
    {
        $Message = "Script: " + $ScriptPath + "`nScript User: " + $Username + "`nFinished: " + (Get-Date).toString()
        Write-EventLog -LogName 'Windows Powershell' -Source $ScriptName -EventID "104" -EntryType "Information" -Message $Message 
        }



Re: Windows Searching, Downloading, and Installing Updates
Source for the ps1 script