Advertisement
yuljk

WindowsUpdates.ps1

May 9th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. This script will automatically install all avaialable windows updates on a device and will automatically reboot if needed, after reboot, windows updates will continue to run until no more updates are available.
  4. .PARAMETER URL
  5. User the Computer parameter to specify the Computer to remotely install windows updates on.
  6. #>
  7.  
  8. [CmdletBinding()]
  9.  
  10. param (
  11.  
  12. [parameter(Mandatory=$true,Position=1)]
  13.  
  14. [string[]]$computer)
  15.  
  16. #install pswindows updates module
  17. $nugetinstall = invoke-command -ComputerName $computer -ScriptBlock {Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force}
  18. invoke-command -ComputerName $computer -ScriptBlock {install-module pswindowsupdate -force}
  19.  
  20. invoke-command -ComputerName $computer -ScriptBlock {Import-Module PSWindowsUpdate -force}
  21.  
  22. Do{
  23.  
  24. #starts up a remote powershell session to the computer
  25. do{$session = New-PSSession -ComputerName $computer
  26. "reconnecting remotely to $computer"
  27. sleep -seconds 10
  28. } until ($session.state -match "Opened")
  29.  
  30. #retrieves a list of available updates
  31.  
  32. "Checking for new updates available on $computer"
  33.  
  34. $updates = invoke-command -session $session -scriptblock {Get-wulist -verbose}
  35.  
  36. #counts how many updates are available
  37.  
  38. $updatenumber = ($updates.kb).count
  39.  
  40. #if there are available updates proceed with installing the updates and then reboot the remote machine
  41.  
  42. if ($updates -ne $null){
  43.  
  44. #remote command to install windows updates, creates a scheduled task on remote computer
  45.  
  46. $Script = {import-module PSWindowsUpdate; Get-WindowsUpdate -AcceptAll -Install | Out-File C:\PSWindowsUpdate.log}
  47.  
  48. Invoke-WUjob -ComputerName $computer -Script $Script -Confirm:$false -RunNow
  49.  
  50. #Show update status until the amount of installed updates equals the same as the amount of updates available
  51.  
  52. sleep -Seconds 30
  53.  
  54. do {$updatestatus = Get-Content \\$computer\c$\PSWindowsUpdate.log
  55.  
  56. "Currently processing the following update:"
  57.  
  58. Get-Content \\$computer\c$\PSWindowsUpdate.log | select-object -last 1
  59.  
  60. sleep -Seconds 10
  61.  
  62. $ErrorActionPreference = ‘SilentlyContinue’
  63.  
  64. $installednumber = ([regex]::Matches($updatestatus, "Installed" )).count
  65.  
  66. $ErrorActionPreference = ‘Continue’
  67.  
  68. }until ( $installednumber -eq $updatenumber)
  69.  
  70. #restarts the remote computer and waits till it starts up again
  71.  
  72. "restarting remote computer"
  73.  
  74. Restart-Computer -Wait -ComputerName $computer -Force
  75.  
  76. }
  77.  
  78. }until($updates -eq $null)
  79.  
  80. #removes schedule task from computer
  81.  
  82. invoke-command -computername $computer -ScriptBlock {Unregister-ScheduledTask -TaskName PSWindowsUpdate -Confirm:$false}
  83.  
  84. "Windows is now up to date on $computer"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement