Using powershell to update Windows servers

Hello,

I am trying to create a powershell script to install all approved windows updates from our WSUS server.

However, when manually checking for updates on said server, if on retry, the script does not force retry of the updates. This is causing a problem. How can I force a “retry” when server calls for it?

Any suggestions on how to force an update when server says Retry update?

Below is my code:

clear
$finished = 0

$n19server = Get-ADComputer -Filter {name -like ‘nw*’}

function Start-Sleep($seconds) {
$doneDT = (Get-Date).AddSeconds($seconds)
while($doneDT -gt (Get-Date)) {
$secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
$percent = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity “Sleeping” -Status “Sleeping…” -SecondsRemaining $secondsLeft -PercentComplete $percent
[System.Threading.Thread]::Sleep(500)
}
Write-Progress -Activity “Sleeping” -Status “Sleeping…” -SecondsRemaining 0 -Completed
}

while ($finished -le 15) {

foreach ($servert in $n19server) {

   [string]$serverName = $servert.name
   $serverName
   Restart-Computer -ComputerName $serverName -Force

}

write-host “sleeping for 10 minutes”
$time = get-date -Format HH:mm:ss
write-host $time
start-sleep(“600”)

foreach ($server in $n19server) {

   [string]$serverName = $server.name
   $serverName
 
    Invoke-Command -ComputerName $serverName -ScriptBlock {
    Get-WindowsUpdate -verbose
    Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Install -AutoReboot
  }

}

write-host “sleeping for 30 minutes”
$time = get-date -Format HH:mm:ss
write-host $time
start-sleep(“1800”)

$finished++
}

Hello and welcome to forums!

The module which you’re using is:
PowerShell Gallery | PSWindowsUpdate 2.2.0.2

From what I see the module does not provide a function for your issue, although you might find some code here:
PowershellModules/PSWindowsUpdate at master · dwj7738/PowershellModules (github.com)

If that’s not useful there is one possible solution but it will require some work:

When you run Get-WindowsUpdateLog -LogPath "path\to\log_file" it will generate a log in said location.

You’ll need to parse this log according to date and search for *FAILED* and similar depending on problem, then if there is fail you can retry command.

By “retry” you mean retry failed updates right?
btw. please edit your post to fix code formatting.