Thursday, July 31, 2014

PowerShell arrays with just one entry are not real arrays

Earlier this year I wrote about ADfind and PowerShell.

http://secattic.blogspot.com/2014/05/combine-adfind-and-powershell.html

So what I found is, that the LDAP result contains only one entry PowerShell is not creating an array, more like an single. string. I am not sure how to explain that better. But what happens is if you run e.g.

$content.length or $content.count it does not return any value at all.

or

if you have code where you want process all entries in a array e.g. with foreach and you access the array like this:

$content[i].uid

it is not working and you see an error "Unable to index into an object..."

So the resulting is to suround the array variable with an @(), e.g. @($content).count

or

$dn=@($content)[i].dn



No comments: