2

So I'm trying to write a powershell script that does the following:

  • Download & install Windows Updates (done)
  • Automatically reboots (done)
  • Checks after the reboot if there are still any updates
  • Goes back to start untill there are no more updates left

I already have constructed my powershell script as following:

Import-Module PSWindowsUpdate

Get-Command –module PSWindowsUpdate

Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d 

Get-WUInstall –MicrosoftUpdate –AcceptAll –AutoReboot

Which works perfect.

However, depending on some things, it's possible that there are still available updates after the reboot that can be downloaded and installed.

What I want to do is to keep running the script above after each reboot untill there are no more updates left to be downloaded & installed.

What I've found out is the Task schedulerin Windows that could be handy.

What I also have constructed is a 'logic' to check if there are any updates left (and install them):

$Output = (Get-WUInstall -MicrosoftUpdate -ListOnly) | Out-String
if($Output.Contains("Update"))
    {
            Write-Host "Updates Available, they will be installed..." 
            Get-WUInstall –MicrosoftUpdate –AcceptAll –AutoReboot
    }else

    {
    Write-Host "There are no updates available. "
    }

ANd the piece code of above also works flawlessly.

My question was to inform in what way I can let this script (or multiple scripts?) run after each reboot untill there are no more updates left...

Thanks

Kahn Kah
  • 1,389
  • 7
  • 24
  • 49

2 Answers2

1

You are on the right track already. Just add a scheduled task to your system that runs powershell.exe with your script as parameter and set the task trigger to "At startup"

TToni
  • 9,145
  • 1
  • 28
  • 42
  • Thank you very much TToni, but I was thinking, doesn't this mean that my script keeps running after each reboot (even when there are no more updates left?). Or are you looking to this as a 'script used on the long term'? – Kahn Kah Feb 17 '17 at 10:41
  • You could add logic to your script that removes the scheduled task when there are no more updates to install, by using this cmdlet: `Unregister-ScheduledTask` (See: https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/16/powertip-use-powershell-to-delete-scheduled-task/) – Mark Wragg Feb 17 '17 at 11:11
  • Thanks Mark! I will check it definately – Kahn Kah Feb 17 '17 at 11:32
1

Boxstarter would manage this more easily than you rolling your own scripts.

Then you save a script with this command:

# Update Windows and reboot if necessary
Install-WindowsUpdate -AcceptEula

Boxstarter can also automate the install and setup of loads of other software, using Chocolatey. See this link for loads of examples.

Failing that working, or if you really want to roll your own update script, you need to look at workflows, which can resume from where they left off after a reboot. Check these two links (link1, link2) out for examples

TechSpud
  • 3,418
  • 1
  • 27
  • 35