Invoke-Comobject

Component Object Model (COM)  is the basis for several other Microsoft technologies and frameworks, including OLEOLE AutomationBrowser Helper ObjectActiveXCOM+DCOM, the Windows shellDirectXUMDF and Windows Runtime. ComObjects can be created with the New-Object cmdlet but the problem is that the input and output are provider through the arguments which makes it difficult pipe or assign values the normal way.

Therefor this little Invoke-ComObject wrapper that takes away some overhead and has a more common PowerShell interface.

Cmdlet

Syntax

Invoke-ComObject [-ComObject] <ComObject|String> [-Method] [-Name] <string> [[-Value] <Object>]

Parameters

[-ComObject] < ComObject|String>

If the supplied argument is a ComObject, the concerned ComObject will be (re)used. If the supplied argument is a String, a new ComObject will be created unless a copy of the concerned ComObject already exist, the original ComObject will be (re)used.

[-Method]

If supplied, the ComObject will be invoked as a method (InvokeMethod). If omitted, the cmdlet will automatically determine whether the property needs to be get (GetProperty) or set (SetProperty) depending on whether a -Value argument is supplied.

[-Name] <string>

  • In case the -Method switch is supplied, the name of the method (InvokeMethod).
  • In case a -Value argument is supplied, the name of the property to be set (SetProperty).
  • In case both the -Method switch and the –Value argument are omitted, the property to be get (GetProperty) and returned by the cmdlet.

[[-Value] <Object>]

The value to be set (SetProperty) or invoked (InvokeMethod).

Examples

With this cmdlet you do not require the to create the ComObject first and retrieve e.g. to get the ComputerName (DN) from ADSystemInfo in a simple oneliner:

Com ADSystemInfo ComputerName

To do a the same with the PathName:

A name NameTranslate example:

Or if you do want to have multiple instances you can first create the ComObject instance and then supply it to the Com function:

The Invoke-ComObject cmdlet was originally published at StackOverflow

 

Leave a Reply