Advertisement
Guest User

up

a guest
Jan 29th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Get and optionally install Windows Updates
  4. .DESCRIPTION
  5. This script will get all available udpates for the computer it is run on.
  6. It will then optionally install those updates, provided they do not require
  7. user input.
  8.  
  9. This script was based off the original vbs that appeared on the MSDN site.
  10. Please see the Related Links section for the URL.
  11.  
  12. Without any parameters the script will return the title of each update that
  13. is currently available.
  14. .PARAMETER Install
  15. When present the script will download and install each update. If the EulaAccept
  16. param has not been passed, only updates that don't have a Eula will be applied.
  17. .PARAMETER EulaAccept
  18. When present will allow the script to download and install all updates that are
  19. currently available.
  20. .EXAMPLE
  21. .\Get-WindowsUpdates.ps1
  22.  
  23. There are no applicable updates
  24.  
  25. Description
  26. -----------
  27. This system is currently patched and up to date.
  28. .NOTES
  29. ScriptName : Get-WindowsUpdates.ps1
  30. Created By : jspatton
  31. Date Coded : 08/29/2012 13:06:31
  32. ScriptName is used to register events for this script
  33.  
  34. ErrorCodes
  35. 100 = Success
  36. 101 = Error
  37. 102 = Warning
  38. 104 = Information
  39. .LINK
  40. https://code.google.com/p/mod-posh/wiki/Production/Get-WindowsUpdates.ps1
  41. .LINK
  42. http://msdn.microsoft.com/en-us/library/windows/desktop/aa387102(v=vs.85).aspx
  43. #>
  44. [CmdletBinding()]
  45. Param
  46. (
  47. [switch]$Install,
  48. [switch]$EulaAccept
  49. )
  50. Begin
  51. {
  52. $ScriptName = $MyInvocation.MyCommand.ToString()
  53. $ScriptPath = $MyInvocation.MyCommand.Path
  54. $Username = $env:USERDOMAIN + "\" + $env:USERNAME
  55.  
  56. New-EventLog -Source $ScriptName -LogName 'Windows Powershell' -ErrorAction SilentlyContinue
  57.  
  58. $Message = "Script: " + $ScriptPath + "`nScript User: " + $Username + "`nStarted: " + (Get-Date).toString()
  59. Write-EventLog -LogName 'Windows Powershell' -Source $ScriptName -EventID "104" -EntryType "Information" -Message $Message
  60.  
  61. # Dotsource in the functions you need.
  62.  
  63. $UpdateSession = New-Object -ComObject 'Microsoft.Update.Session'
  64. $UpdateSession.ClientApplicationID = 'MSDN PowerShell Sample'
  65. }
  66. Process
  67. {
  68. $UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
  69. $SearchResult = $UpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
  70.  
  71. if ($Install)
  72. {
  73. Write-Verbose 'Creating a collection of updates to download:'
  74. $UpdatesToDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'
  75. foreach ($Update in $SearchResult.Updates)
  76. {
  77. [bool]$addThisUpdate = $false
  78. if ($Update.InstallationBehavior.CanRequestUserInput)
  79. {
  80. Write-Verbose "> Skipping: $($Update.Title) because it requires user input"
  81. }
  82. else
  83. {
  84. if (!($Update.EulaAccepted))
  85. {
  86. Write-Verbose "> Note: $($Update.Title) has a license agreement that must be accepted:"
  87. if ($EulaAccept)
  88. {
  89. $Update.AcceptEula()
  90. [bool]$addThisUpdate = $true
  91. }
  92. else
  93. {
  94. Write-Verbose "> Skipping: $($Update.Title) because the license agreement was declined"
  95. }
  96. }
  97. else
  98. {
  99. [bool]$addThisUpdate = $true
  100. }
  101. }
  102. if ([bool]$addThisUpdate)
  103. {
  104. Write-Verbose "Adding: $($Update.Title)"
  105. $UpdatesToDownload.Add($Update) |Out-Null
  106. }
  107. }
  108. if ($UpdatesToDownload.Count -eq 0)
  109. {
  110. Write-Verbose 'All applicable updates were skipped.'
  111. break
  112. }
  113.  
  114. Write-Verbose 'Downloading updates...'
  115. $Downloader = $UpdateSession.CreateUpdateDownloader()
  116. $Downloader.Updates = $UpdatesToDownload
  117. $Downloader.Download()
  118.  
  119. $UpdatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'
  120.  
  121. [bool]$rebootMayBeRequired = $false
  122. Write-Verbose 'Successfully downloaded updates:'
  123.  
  124. foreach ($Update in $SearchResult.Updates)
  125. {
  126. if ($Update.IsDownloaded)
  127. {
  128. Write-Verbose "> $($Update.Title)"
  129. $UpdatesToInstall.Add($Update) |Out-Null
  130.  
  131. if ($Update.InstallationBehavior.RebootBehavior -gt 0)
  132. {
  133. [bool]$rebootMayBeRequired = $true
  134. }
  135. }
  136. }
  137. if ($UpdatesToInstall.Count -eq 0)
  138. {
  139. Write-Verbose 'No updates were succsesfully downloaded'
  140. }
  141.  
  142. if ($rebootMayBeRequired)
  143. {
  144. Write-Verbose 'These updates may require a reboot'
  145. }
  146.  
  147. Write-Verbose 'Installing updates...'
  148.  
  149. $Installer = $UpdateSession.CreateUpdateInstaller()
  150. $Installer.Updates = $UpdatesToInstall
  151. $InstallationResult = $Installer.Install()
  152.  
  153. Write-Verbose "Installation Result: $($InstallationResult.ResultCode)"
  154. Write-Verbose "Reboot Required: $($InstallationResult.RebootRequired)"
  155. Write-Verbose 'Listing of updates installed and individual installation results'
  156.  
  157. for($i=0; $i -lt $UpdatesToInstall.Count; $i++)
  158. {
  159. New-Object -TypeName PSObject -Property @{
  160. Title = $UpdatesToInstall.Item($i).Title
  161. Result = $InstallationResult.GetUpdateResult($i).ResultCode
  162. }
  163. }
  164. }
  165. else
  166. {
  167. if ($SearchResult.Updates.Count -ne 0)
  168. {
  169. $SearchResult.Updates |Select-Object -Property Title, Description, SupportUrl, UninstallationNotes, RebootRequired |Format-List
  170. }
  171. else
  172. {
  173. Write-Host 'There are no applicable updates'
  174. }
  175. }
  176. }
  177. End
  178. {
  179. $Message = "Script: " + $ScriptPath + "`nScript User: " + $Username + "`nFinished: " + (Get-Date).toString()
  180. Write-EventLog -LogName 'Windows Powershell' -Source $ScriptName -EventID "104" -EntryType "Information" -Message $Message
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement