Powershell - why could this line be failing to run on some servers

I am trying to audit the number of pending updates for each server using Get-PendingUpdates found in the gallery, but this script fails to run on a some of our 2016 servers.

(script below)

Problematic servers pause indefinately whilst running the following line
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")

Open in new window


Most servers run the query OK in 2-3 seconds, however some of our servers pause on is this line indefinitely:

Other key points:
Same problem using PSSession AND\OR locally using Powershell (running as admin).
Powershell.exe in taskmanager CPU% is 0. Private working memory 26,748 (doesnt move)

Our servers are all deployed from the same Server 2016 template so it doesnt make sense to me why some would be OK and others not.

Can anyone offer any troubleshooting tips?. Possible causes.  Rebooting servers is not really an option at the moment.
Cheers
String


#Get All Assigned updates in $SearchResult
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")
#Matrix Results for type of updates that are needed
$Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
$other = $SearchResult.updates | where { $_.MsrcSeverity -eq $null }

#Write Results
Write-Host "total=$($SearchResult.updates.count)"
Write-Host "critical=$($Critical.count)"
Write-Host "important=$($Important.count)"
Write-Host "other=$($other.count)"

Open in new window

String :-)Windows EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DevAdminSystem Engineer | .NET Developer | Microsoft MVP | Technical SpeakerCommented:
I already use scripts similar... but I remeber well it is necessary configure Windows Update to check the updates... compare the settings of Windows Update with the servers on witch the script run without problems..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
String :-)Windows EngineerAuthor Commented:
Hey DevAdmin  - Actually you are correct. The servers this is hanging on, werent getting the GPO that set the WSUS server and therefore showed MS updates werent running at all. It was a good catch this one!!
Cheers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.