PoshCode Archive  Artifact [b32c7edb62]

Artifact b32c7edb6288db67064ae1af4935d311f6810708119f44e8c1d41feca0420bed:

  • File ComObjects-Types.ps1 — part of check-in [e4a187e741] at 2018-06-10 13:03:51 on branch trunk — 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. (user: unknown size: 2200)

# 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>