Last active
April 18, 2026 18:27
-
-
Save AfroThundr3007730/6161afd662a2811b1c95ee6fb64e2a48 to your computer and use it in GitHub Desktop.
Several powershell functions to invoke methods on raw COM objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Several powershell functions to invoke methods on raw COM objects | |
| # Last updated 2026-04-18 by AfroThundr | |
| # From: https://stackoverflow.com/questions/20248032/ | |
| Set-StrictMode -Version Latest | |
| function Invoke-ComObject { | |
| <# .SYNOPSIS | |
| Invoke a method on a COM object or get/set a property #> | |
| [CmdletBinding()] | |
| param ( | |
| # The COM object or name of said object | |
| [Parameter(Mandatory)] | |
| [__ComObject]$ComObject, | |
| # Invoke a method on the COM object | |
| [Parameter()] | |
| [Switch]$Method, | |
| # Name of the method or property to access | |
| [Parameter(Mandatory)] | |
| [String]$Name, | |
| # Value of the property, or method parameters | |
| [Parameter()] | |
| [object]$Value | |
| ) | |
| if ($ComObject -isnot '__ComObject') { | |
| if (!$ComInvoke) { $Global:ComInvoke = @{} } | |
| if (!$ComInvoke.$ComObject) { | |
| $ComInvoke.$ComObject = New-Object -ComObject $ComObject | |
| } | |
| $ComObject = $ComInvoke.$ComObject | |
| } | |
| if ($Method) { $Invoke = 'InvokeMethod' } elseif ( | |
| $MyInvocation.BoundParameters.ContainsKey('Value') | |
| ) { $Invoke = 'SetProperty' } else { $Invoke = 'GetProperty' } | |
| $ComObject.GetType().InvokeMember($Name, | |
| $Invoke, $Null, $ComObject, $Value) | |
| } | |
| function Get-ComProperty { | |
| <# .SYNOPSIS | |
| Get a property of a ComObject #> | |
| param( | |
| # The COM object or name of said object | |
| [Parameter(Mandatory)] | |
| [__ComObject]$ComObject, | |
| # Name of the property to access | |
| [Parameter(Mandatory)] | |
| [String]$PropertyName | |
| ) | |
| $ComObject.GetType().InvokeMember($PropertyName, | |
| 'GetProperty', $Null, $ComObject, $Null) | |
| } | |
| function Set-ComProperty { | |
| <# .SYNOPSIS | |
| Set a property of a ComObject #> | |
| param( | |
| # The COM object or name of said object | |
| [Parameter(Mandatory)] | |
| [__ComObject]$ComObject, | |
| # Name of the property to access | |
| [Parameter(Mandatory)] | |
| [String]$PropertyName, | |
| # Value of the property to set | |
| [Parameter(Mandatory)] | |
| [object]$PropertyValue | |
| ) | |
| $ComObject.GetType().InvokeMember($PropertyName, | |
| 'SetProperty', $Null, $ComObject, $PropertyValue) | |
| } | |
| function Invoke-ComMethod { | |
| <# .SYNOPSIS | |
| Invoke a method of a ComObject #> | |
| param( | |
| # The COM object or name of said object | |
| [Parameter(Mandatory)] | |
| [__ComObject]$ComObject, | |
| # Name of the method to invoke | |
| [Parameter(Mandatory)] | |
| [String]$MethodName, | |
| # Parameters of the method to invoke | |
| [Parameter()] | |
| [object]$MethodParameters = $Null | |
| ) | |
| $ComObject.GetType().InvokeMember($MethodName, | |
| 'InvokeMethod', $Null, $ComObject, $MethodParameters) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment