Question

Powershell COM Help


Userlevel 4
Badge +2

Forgive me, as this isn’t strictly a Veeam topic, but I’m pulling my hair out.

I’m trying to enhance a Windows Update Powershell script. The current script occasionally hangs when we invoke a COM CreateUpdateSearcher() method.

$updateSession = new-object -com "Microsoft.Update.Session"
$Criteria = "IsInstalled=0"
$updates = $updateSession.CreateupdateSearcher().Search($criteria).Updates

This runs synchronously, so I can’t trap the hang. Instead, I want to use the BeginSearch() method that runs asynch (https://learn.microsoft.com/en-us/windows/win32/api/wuapi/nf-wuapi-iupdatesearcher-beginsearch).

 

My problem is it requires an ‘IUnknown ISearchCompletedCallback’ object to be passed as one of the parameters. I don’t need this to be invoked, so it can be an empty object, but I can’t work out how to create an empty IUnknown object.

$UpdateSession = New-Object -ComObject "Microsoft.Update.Session"
$objSearcher = $UpdateSession.CreateUpdateSearcher()
$objSearch = $objSearcher.BeginSearch('IsInstalled=0',??,$null)

 

Can anyone help please!

Rob.


2 comments

Userlevel 6
Badge +5

Hi Rob,

Have you tried using PowerShell’s built-in job functionality? This would force the process to the background and then you can monitor the job.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job?view=powershell-5.1

 

Hope this helps!

Chris

Userlevel 4
Badge +2

That’s my Plan-B Chris if I can’t work out the New-Object syntax, but I hate to be beaten into submission 😋

 

Rob.

Comment