title: Powershell - converting a COM object type to a string
body: <p>We received this 'new' COM object dll (why not .NET is beyond me) from the vendor for one of our business applications a few days back. Due to the extreme lack of documentation (no example or instructions on how to integrate the COM object to the existing system), we've been struggling to integrate it into our existing infrastructure. </p>
  
  <p>Here I am trying to use Powershell to access a method called 'API' in the COM object class and read the results returned by it. The method is used to call the various APIs depending on what we want to do. The parameters for any given API are passed in as an object array. The returned value is stored in a custom type called colGeneric, which is essentially an array with key/value pairs (with the custom type clsGeneric). In this case it returns two pairs - pb_result and pb_resulttext.</p>
  
  <pre><code>$comClass = new-object -comObject VendorCOM.VendorClass
  $APIName = "SomeAPI"
  $ArrayOfParamsNeededByAPI = 12345, "ABC", "2011-08-01"
  
  $resultSet = $comClass.API([REF] $APIName, [REF] $ArrayOfParamsNeededByAPI)
  
  $resultText = $resultSet.Item([ref] "pb_resulttext")
  </code></pre>
  
  <p>When I try to print out $clsGenObjResultText, it shows up as System.__ComObject. I know it is a string, but attempts to convert to a string has failed so far. Here is one thing I've tried:</p>
  
  <pre><code>[System.Convert]::ToString($resultText)
  </code></pre>
  
  <p>It doesn't work as it still returns System.__ComObject. Not sure if I am making sense here, but I just need the result text in string form. </p>
  
  <p><strong>UPDATE</strong> Thanks to the diligent people on this website, the correct solution to the above issue is to replace the last statement in the code snippet with:</p>
  
  <pre><code>$resultText = $resultSet.Item([ref] "pb_resulttext").Value
  </code></pre>