0

I converted a VBScript to a Powershell instead. This is one of my many problems that I encountered:

Method invocation failed because [ADODB.CommandClass] doesn't contain a method named 'Properties'.

Code Below:

    $objConnection = new-Object  -com "ADODB.Connection"
    $objCommand = new-Object -com "ADODB.Command"

    $objConnection.Provider = "ADsDSOObject"
    $objConnection.Open( "Active Directory Provider")
    $objCommand.ActiveConnection = $objConnection

    $objCommand.CommandText = "Select Name From '" + $ADSPath + "' where " + $SearchField + " = '" + $SearchValue + "'"
    $objCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_EXTERNAL

The problem might be because of the ADS_CHASE_REFERRALS_EXTERNAL only working for VB. Any solution or the proper way to do this in Powershell?

Sid
  • 765
  • 5
  • 29
  • 57

1 Answers1

0

The Properties member is not a function, but a powershell property:

# ~> $objCommand | get-member Properties


   TypeName: System.__ComObject#{986761e8-7269-4890-aa65-ad7c03697a6d}

Name       MemberType Definition                     
----       ---------- ----------                     
Properties Property   Properties Properties () {get} 

From the signature, you can see that it returns a Properties Object which is a collection of Com objects, each of which is a com object with the following members:

# ~> $objCommand.Properties | get-member


   TypeName: System.__ComObject#{00000503-0000-0010-8000-00aa006d2ea4}

Name       MemberType Definition                    
----       ---------- ----------                    
Attributes Property   int Attributes () {get} {set} 
Name       Property   string Name () {get}          
Type       Property   DataTypeEnum Type () {get}    
Value      Property   Variant Value () {get} {set}  

The simplest way I can figure out to get the property you are interested in, is to loop through the returned objects, find the property with the name you are interested in, then set the value as you wish:

($objCommand.Properties | where{$_.Name -eq "Chase referrals"}).Value = ADS_CHASE_REFERRALS_EXTERNAL

That's a bit awkward if you have to do it often, so perhaps you can turn it into a function:

function Set-ADOCommandPreoprty($objCOmmand, [string]$propertyName, $newValue)
{
    ($objCommand.Properties | ?{$_.Name -eq $propertyName}).Value = $newValue
}

Then call it like this:

Set-ADOCommandPreoprty $objCommand "Chase referrals" ADS_CHASE_REFERRALS_EXTERNAL
zdan
  • 28,667
  • 7
  • 60
  • 71