Function returns using ADSI

I have a function which does adsi updates called set-adsiobj
The main code is here:

    try {
        $adobj = [ADSI]"LDAP://$ObjectDN"
        $adobj.PutEx($PutexAction, $Attribute, @($Value))
        $adobj.setinfo()
        return $true
    }
    catch {
        $global:SetAdsiError = $_
        return $false
    }

If I call a function like this with valid params: $result = get-adsiobj …
… the value of $result is weird
i.e. if the setinfo succeeds, $result is a two element array with the second element = $true
if the setinfo fails, $result is a three element array with the third element = $false

Any idea of whats happening or how I can get this to return simply true or false?

btw - the other elements in the array are $null. Its like the putex or setinfo are returning null values?

The methods you’re using have output, and in PowerShell all unassigned output is returned. If you don’t want the data assign it, or pipe it, to null.

# Assigned example
$null = $adobj.PutEx($PutexAction, $Attribute, @($Value))
# Piped example
$adobj.setinfo() | Out-Null

Because all output is returned by default, in PowerShell it’s considered a best practice to not explicitly declare the return keyword unless you’re using it to exit a function at a specific point.

try {
    $adobj = [ADSI]"LDAP://$ObjectDN"
    $null = $adobj.PutEx($PutexAction, $Attribute, @($Value))
    $null = $adobj.setinfo()
    $true
}
catch {
    $global:SetAdsiError = $_
    $false
}
1 Like