I need to List all updates on Windows that are not installed and write them to a file. But if it takes too long there must be a timeout.
I tried to run a search with a Update Searcher Object as a Job and then continue either when it's completed or the timeout takes place.
Then I check if the job has been completed or not. If it did I pass the job to Receive-Job
to get its result and write it to the file.
$session = New-Object -ComObject "Microsoft.Update.Session"
$searcher = $session.CreateUpdateSearcher()
$j = Start-Job -ScriptBlock {
$searcher.Search("IsInstalled=0").Updates | Select-Object Type, Title, IsHidden
} | Wait-Job -Timeout 120
if ($j.State -eq 'Completed') {
Receive-Job $j -Keep | Out-File @out_options
} else {
echo "[TIMEOUT] Script took too long to fetch all missing updates." |
Out-File @out_options
}
@out_options
are defined, if you should wonder.
The only thing I receive is following error:
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
By now I figured out that the error stems from calling Receive-Job
. It seems the job is completing before there is a result.
How do I receive Result from my background job?