1

I'm using the wuapi to write a script that scans, downloads, and installs updates for a machine that is not connected to the Internet. I have most of this script completed, but I need to be able to do a cast (I believe) to a different object.

I found code here that does exactly what I need, but in C#:
WUApiLib IUpdateInstaller2 yields Error; Some OS updates install others throw HResult -2145124318

And here is what the code looks like:

var collection = new UpdateCollection();
IList<string> updateFiles = Directory.GetFiles(updateFolder);
var fileCollection = new StringCollection();

foreach (var file in updateFiles)
    fileCollection.Add(file);

//Error happens here on certain updates. Not all.

/** THIS LINE BELOW IS THE ONE I NEED **/
((IUpdate2)update.BundledUpdates[0]).CopyToCache(fileCollection);
collection.Add(update);
return collection;

The line ((IUpdate2)update.BundledUpdates[0]).CopyToCache(fileCollection); returns an IUpdate object, but in order to use the CopyToCache function, it needs to be an IUpdate2 object. I just can't seem to figure out how to get this part working as powershell gives me a "load library" type of error.

Does anyone know how I can do this in PowerShell?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
nullByteMe
  • 6,141
  • 13
  • 62
  • 99

1 Answers1

2

If the problem is that the method CopyToCache is implemented as an explicit interface implementation, maybe try

[IUpdate2].GetMethod("CopyToCache").Invoke($update.BundledUpdates[0], @($fileCollection))

or similar.

See How can I call explicitly implemented interface method from PowerShell?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181