Yep, this is a PowerShell v2 annoyance: scalar values can behave
slightly differently to array values.
Assuming Windows 8 or PS v3 beta, you can see the difference:
> powershell -version 2 -command { $x = 1,2,3; write-host 'V2
Array:',($x.GetType().Name),($x.Count -eq $null),($x.Count) }
V2 Array: Object[] False 3
> powershell -version 2 -command { $x = 4; write-host 'V2
Scalar:',($x.GetType().Name),($x.Count -eq $null),($x.Count) }
V2 Scalar: Int32 True
With Powershell v3 it's improved, the Count property is automagically
generated for convenience:
> powershell -command { $x = 1,2,3; write-host 'V3
Array:',($x.GetType().Name),($x.Count -eq $null),($x.Count) }
V3 Array: Object[] False 3
> powershell -command { $x = 4; write-host 'V3
Scalar:',($x.GetType().Name),($x.Count -eq $null),($x.Count) }
V3 Scalar: Int32 False 1
By sending the results through the pipeline it looks like you're
generating either an array or a scalar, and only the former has a Count
property in PS V2.
Whereas if you assign the return value of FindAll() directly to $result
you have a System.DirectoryServices.SearchResultCollection object that
always has a Count property.
A few solutions you could use are:
a) Use the directory searcher itself to do the sort:
$searcher.Sort.PropertyName = 'sn'
$searcher.Sort.Direction = 'Ascending';
b) Sort later:
$results | ForEach-Object {
$item = $_.Properties;
New-Object PSObject -Property @{
SN=$item.sn | select -first 1;
GivenName=$item.givenname | select -first 1;
... etc etc ...
}} | Sort-Object SN
c) Use Measure-Object to get the count
$resultCount = ($results | Measure-Object).Count
Cheers,
Tony
On 22/08/2012 3:44 PM, Chris Tomich wrote:
Heya Paul,
I think it's to do with your use of the Sort-Object expression. I just
did some testing with it and it seems that if you pipe in an array
with a single object in it, it will return the single object type not
in an array. Below is what I tested.
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\Chris> $array = @(5, 3, 6, 8, 0)
PS C:\Users\Chris> $array
5
3
6
8
0
PS C:\Users\Chris> $array.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\Users\Chris> $sorted = $array | Sort-Object
PS C:\Users\Chris> $sorted
0
3
5
6
8
PS C:\Users\Chris> $sorted.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\Users\Chris> $singleValueArray = @(4)
PS C:\Users\Chris> $singleValueArray
4
PS C:\Users\Chris> $singleValueArray.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\Users\Chris> $sorted = $singleValueArray | Sort-Object
PS C:\Users\Chris> $sorted
4
PS C:\Users\Chris> $sorted.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
PS C:\Users\Chris>
Kind Regards,
Chris Tomich
On 22 August 2012 07:14, Web Admin <web.ad...@syd.catholic.edu.au
<mailto:web.ad...@syd.catholic.edu.au>> wrote:
Hi all,
I've written an AD search script and am having a strange problem
with returning the result count when ONE item is returned.
If one result is returned I get no value displayed for
$results.count. Anything greater than "one" returns the correct count.
Moreover, in order to return "one" result I need to use *-ge -1*
in the if/else statement. [see below]
Is this a known issue or can someone tell me what value is/should
be returned for a single result?
$results = $searcher.FindAll() | Sort-Object
@{Expression={$_.Properties.sn}}
if ($results.count -ge -1)
{
# Format our results
cls
Write-Host "Results for $field = $value." $results.count "results
found."
ForEach ($user in $results)
{
GetUserInfo `
$user.properties.sn <http://properties.sn> `
$user.properties.givenname `
$user.properties.mail `
$user.properties.employeeid `
$user.properties.department `
$user.properties.telephonenumber `
$user.properties.wwwhomepage
}
}
else
{
cls
Write-Host "No results found. Try to broaden your search scope."
}
Regards,
Paul Noone
--
SharePoint Farm Admin/Developer
Infrastructure Team
CEO Sydney
p: (02) 9568 8461
f: (02) 9568 8483
e:paul.no...@syd.catholic.edu.au
<mailto:paul.no...@syd.catholic.edu.au>
w:http://www.ceosyd.catholic.edu.au/
_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com <mailto:ozmoss@ozmoss.com>
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss