PowerShell and COM objects.

While developing a COM object for scripting use I wanted to provide a VBScript sample as well as a PowerShell sample.

While creating the PowerShell sample I received an unexpected error message when setting a property value. Some quick review shows that PowerShell 1.0 (I did not check with the 2.0 CTP yet) cannot handle write-only COM properties.

Consider the following PowerShell script:

$obj = New-Object -comObject “PropertyTest.PropTest”
$obj.ReadWrite = “Some text”
$readonly = $obj.ReadOnly
$obj.WriteOnly = “some more text”

The object’s properties behave as they are named. If you run these instructions one by one in PowerShell you get the  following error message:

Exception setting “WriteOnly”: “Type mismatch. (Exception from HRESULT: 0x80020
005 (DISP_E_TYPEMISMATCH))”
At line:1 char:6
+ $obj.W <<<< riteOnly = “some more text”

Read-write and read-only properties work fine, only write-only properties are affected. I have uploaded the test component here: PropertyTest.dll

Obviously you need to run regsvr32 PropertyTest.dll if you want to test this.