# encoding: ascii
# api: xml
# title: ComObjects.Types
# description: This is a .ps1xml file for use with Update-TypeData … it adds methods onto the COM object for GetProperty (including support for parametrized properties), SetProperty, and InvokeMethod.
# version: 0.1
# type: script
# license: CC0
# x-poshcode-id: 1998
# x-archived: 2010-07-23T17:49:46
#
# I wish this was not necessary, but it seems to be the only way to access methods on COM objects that are exposed through interfaces. For example, WindowsInstaller.Installer only works this way:
# Update-TypeData -Prepend .\ComObject.Types.ps1xml
# $installer = New-Object -Com WindowsInstaller.Installer
# foreach($product in $installer.GetProperty("Products")) {
# New-Object PSObject -Property @{
# ProductName = $installer.GetProperty("ProductInfo",$product,"ProductName")
# VersionString = $installer.GetProperty("ProductInfo",$product,"VersionString")
# }
# }
#
<Types>
<Type>
<Name>System.__ComObject</Name>
<Members>
<ScriptMethod>
<Name>GetProperty</Name>
<Script>
param([Parameter(Mandatory=$true,Position=1)]$PropertyName)
Write-Verbose "PropertyName: $PropertyName"
Write-Verbose "Arguments: $($Args | Out-String)"
$this.gettype().invokeMember($PropertyName,[System.Reflection.BindingFlags]::GetProperty,$null,$this,@($Args))
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>SetProperty</Name>
<Script>
param([Parameter(Mandatory=$true,Position=1)]$PropertyName)
$this.gettype().invokeMember($PropertyName,[System.Reflection.BindingFlags]::SetProperty,$null,$this,@($Args))
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>InvokeMethod</Name>
<Script>
param([Parameter(Mandatory=$true,Position=1)]$MethodName)
$this.gettype().invokeMember($MethodName,[System.Reflection.BindingFlags]::InvokeMethod,$null,$this,@($Args))
</Script>
</ScriptMethod>
</Members>
</Type>
</Types>