Search code examples
powershellscriptingscheduled-tasksscheduledexecutorservice

Using the task scheduler to start a PowerShell script after reboot


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


Solution

  • 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"