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.
Thanks - I thought it was something like that. Interesting about best practice on returning values. I would have to disagree as having a return statement clearly indicates to code readers what the function is meant to return.
For code clarity, I often use Write-Output instead of the code reading $true or $false, or using return. So using “Write-Output $true” makes it clear that the script is putting data into the ‘success’ output stream - in your case writing $true or $false.
For educational purposes, Write-Output and return do behave differently. A couple of differences:
Execution Flow: Write-Output allows the script to continue running after it sends the output. Return exits the function or script immediately. Intent: Write-Output is generally used for sending data to the pipeline. Return is used to exit a function and optionally return a value.