ComObjects.Types (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1998"></script>download | new post
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.
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>
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository