Neobyte
Neobyte

Reputation: 396

Powershell being too clever

Apologies for what is probably a newbish question.

I am writing some Powershell scripts that run various queries against AD. They will usually return a bunch of results, which are easy to deal with, ie:

$results = myQuery
write-host "Returned " + $results.Count + " results."
foreach ($result in $results) { doSomething }

No worries. However, if there is only 1 result, Powershell automagically translates that result into a single object, rather than an array that contains 1 object. So the above code would break, both at the Count and the foreach. I'm sure the same problem would occur with 0 results.

Could someone suggest an elegant way to handle this? Perhaps some way to cast the results so they are always an array?

Upvotes: 7

Views: 328

Answers (3)

Nick
Nick

Reputation:

Actually, the foreach works just fine. All uses of foreach (the foreach keyword, the Foreach-Object cmdlet, and Foreach-Object's aliases "foreach" and "%") all have the same behavior of "wrapping" the object in question in an array if needed. Thus, all uses of foreach will work with both scalar and array values.

Annoyingly, this means they work with null values too. Say I do:

$x = $null
foreach ($y in $x) {Write-Host "Hello world 1"}
$x | Foreach-Object {Write-Host "Hello world 2"}

I'll get

"Hello world 1"
"Hello world 2"

out of that.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180788

Change the first line of code to

$results = @(myQuery)

This will always return an array. See this blog entry for additional details.

Upvotes: 11

Scott Weinstein
Scott Weinstein

Reputation: 19117

This has bitten me as well. No clever ideas on how to fix $results.Count, but the foreach can be fixed by switching to a pipeline.

$scalar = 1
$list = (1,2)
$list | % { $_ }

prints 1 2

$scalar | % { $_ }

prints 1

Upvotes: 0

Related Questions