Advertisement
Yevrag35

Install Updates - Native

Feb 17th, 2020
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Install-Updates()
  2. {
  3.     [CmdletBinding(SupportsShouldProcess = $true, DefaultParameterSetName = "None")]
  4.     param
  5.     (
  6.         [Parameter(ParameterSetName = "DownloadOnly")]
  7.         [switch] $DownloadOnly,
  8.  
  9.         [Parameter(ParameterSetName = "InstallOnly")]
  10.         [switch] $InstallOnly,
  11.  
  12.         [switch] $IncludeHidden
  13.     )
  14.  
  15.     Function Private:Cleanup
  16.     {
  17.         [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($session)
  18.         if ($null -ne $updates)
  19.         {
  20.             [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($updates)
  21.         }
  22.         if ($null -ne $needToDownload)
  23.         {
  24.             [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($needToDownload)
  25.         }
  26.         if ($null -ne $needToInstall)
  27.         {
  28.             [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($needToInstall)
  29.         }
  30.     }
  31.  
  32.     $session = New-Object -ComObject Microsoft.Update.Session
  33.     $searcher = $session.CreateUpdateSearcher()
  34.  
  35.     $searchStr = "IsInstalled=0 AND IsHidden={0}" -f ([System.Convert]::ToInt32($IncludeHidden.ToBool()))
  36.     $updates = $searcher.Search($searchStr).Updates
  37.  
  38.     $needToDownload = New-Object -ComObject Microsoft.Update.UpdateColl
  39.     $needToInstall = New-Object -ComObject Microsoft.Update.UpdateColl
  40.     if ($updates.Count -gt 0)
  41.     {
  42.         $displayList = New-Object 'System.Collections.Generic.List[object]' -ArgumentList $updates.Count
  43.         for ($i = 1; $i -le $updates.Count; $i++)
  44.         {
  45.             $update = $updates[$i - 1]
  46.             $displayList.Add([pscustomobject]@{
  47.                     ID         = $i
  48.                     KBArticle  = $update.KBArticleIDs -join ', '
  49.                     Downloaded = $update.IsDownloaded
  50.                     Title      = $update.Title
  51.                 })
  52.             if ($update.IsDownloaded)
  53.             {
  54.                 [void] $needToInstall.Add($update)
  55.             }
  56.             else
  57.             {
  58.                 [void] $needToDownload.Add($update)
  59.             }
  60.         }
  61.     }
  62.     else
  63.     {
  64.         Write-Host "No updates are needed." -f Green
  65.         return
  66.     }
  67.  
  68.     Write-Host $($displayList | Sort-Object KBArticle | Out-String) -f Cyan
  69.  
  70.     if ($PSCmdlet.ShouldProcess(("Number of updates: {0}" -f $updates.Count), "Process updates"))
  71.     {
  72.         if ($needToDownload.Count -gt 0 -and -not $InstallOnly)
  73.         {
  74.             Write-Host "`nDownloading updates that haven't yet..." -f Yellow -NoNewline
  75.             $downloader = $session.CreateUpdateDownloader()
  76.             $downloader.Updates = $needToDownload
  77.             $result = $downloader.Download()
  78.             if (-not (($result.Hresult -eq 0) -and ($result.ResultCode -eq 2)))
  79.             {
  80.                 Write-Host "FAIL" -f Red
  81.                 Write-Error "Updates could not be downloaded." -Category InvalidResult
  82.             }
  83.             else
  84.             {
  85.                 Write-Host "DONE!" -f Green
  86.                 for ($u = 0; $u -lt @($downloader.Updates).Count; $u++)
  87.                 {
  88.                     $dlup = $downloader.Updates.Item($u)
  89.                     [void] $needToInstall.Add($dlup)
  90.                 }
  91.             }
  92.             Start-Sleep -Seconds 2
  93.         }
  94.  
  95.         if ($needToInstall.Count -gt 0 -and -not $DownloadOnly)
  96.         {
  97.             Write-Host "Installing updates..." -f Yellow -NoNewline
  98.             $installer = $session.CreateUpdateInstaller()
  99.             $installer.Updates = $needToInstall
  100.             $installResult = $installer.Install();
  101.             if (-not ($installResult.Hresult -eq 0) -and ($installResult.ResultCode -eq 2))
  102.             {
  103.                 Write-Host "DONE!" -f Green
  104.             }
  105.             else
  106.             {
  107.                 Write-Host "FAIL" -f Red
  108.                 Write-Error ("Failed to install updates.  Error Code: {0}" -f $installResult.Hresult) -Category InvalidResult
  109.             }
  110.             if ($installResult.RebootRequired)
  111.             {
  112.                 Write-Warning "A reboot is required to finish the installation."
  113.             }
  114.         }
  115.     }
  116.     Private:Cleanup
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement