Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Install-Updates()
- {
- [CmdletBinding(SupportsShouldProcess = $true, DefaultParameterSetName = "None")]
- param
- (
- [Parameter(ParameterSetName = "DownloadOnly")]
- [switch] $DownloadOnly,
- [Parameter(ParameterSetName = "InstallOnly")]
- [switch] $InstallOnly,
- [switch] $IncludeHidden
- )
- Function Private:Cleanup
- {
- [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($session)
- if ($null -ne $updates)
- {
- [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($updates)
- }
- if ($null -ne $needToDownload)
- {
- [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($needToDownload)
- }
- if ($null -ne $needToInstall)
- {
- [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($needToInstall)
- }
- }
- $session = New-Object -ComObject Microsoft.Update.Session
- $searcher = $session.CreateUpdateSearcher()
- $searchStr = "IsInstalled=0 AND IsHidden={0}" -f ([System.Convert]::ToInt32($IncludeHidden.ToBool()))
- $updates = $searcher.Search($searchStr).Updates
- $needToDownload = New-Object -ComObject Microsoft.Update.UpdateColl
- $needToInstall = New-Object -ComObject Microsoft.Update.UpdateColl
- if ($updates.Count -gt 0)
- {
- $displayList = New-Object 'System.Collections.Generic.List[object]' -ArgumentList $updates.Count
- for ($i = 1; $i -le $updates.Count; $i++)
- {
- $update = $updates[$i - 1]
- $displayList.Add([pscustomobject]@{
- ID = $i
- KBArticle = $update.KBArticleIDs -join ', '
- Downloaded = $update.IsDownloaded
- Title = $update.Title
- })
- if ($update.IsDownloaded)
- {
- [void] $needToInstall.Add($update)
- }
- else
- {
- [void] $needToDownload.Add($update)
- }
- }
- }
- else
- {
- Write-Host "No updates are needed." -f Green
- return
- }
- Write-Host $($displayList | Sort-Object KBArticle | Out-String) -f Cyan
- if ($PSCmdlet.ShouldProcess(("Number of updates: {0}" -f $updates.Count), "Process updates"))
- {
- if ($needToDownload.Count -gt 0 -and -not $InstallOnly)
- {
- Write-Host "`nDownloading updates that haven't yet..." -f Yellow -NoNewline
- $downloader = $session.CreateUpdateDownloader()
- $downloader.Updates = $needToDownload
- $result = $downloader.Download()
- if (-not (($result.Hresult -eq 0) -and ($result.ResultCode -eq 2)))
- {
- Write-Host "FAIL" -f Red
- Write-Error "Updates could not be downloaded." -Category InvalidResult
- }
- else
- {
- Write-Host "DONE!" -f Green
- for ($u = 0; $u -lt @($downloader.Updates).Count; $u++)
- {
- $dlup = $downloader.Updates.Item($u)
- [void] $needToInstall.Add($dlup)
- }
- }
- Start-Sleep -Seconds 2
- }
- if ($needToInstall.Count -gt 0 -and -not $DownloadOnly)
- {
- Write-Host "Installing updates..." -f Yellow -NoNewline
- $installer = $session.CreateUpdateInstaller()
- $installer.Updates = $needToInstall
- $installResult = $installer.Install();
- if (-not ($installResult.Hresult -eq 0) -and ($installResult.ResultCode -eq 2))
- {
- Write-Host "DONE!" -f Green
- }
- else
- {
- Write-Host "FAIL" -f Red
- Write-Error ("Failed to install updates. Error Code: {0}" -f $installResult.Hresult) -Category InvalidResult
- }
- if ($installResult.RebootRequired)
- {
- Write-Warning "A reboot is required to finish the installation."
- }
- }
- }
- Private:Cleanup
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement