Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Sorting output from foreach

Closed Thread
 
Thread Tools Display Modes
Old 03-09-2007   #1 (permalink)
SteveHodd
Guest
 
Posts: n/a

Sorting output from foreach

Hi,

I'm quite new powershell.
I've written a script that will list the SamAccount and DisplayName of all
members of an AD group. I want to sort the output from the foreach statement
by the SamAccount field, but I can't get it to work. Does anyone have any
ideas or a better way to do this?

#############
# this line returns members FQDN as an array
$grp = New-Object DirectoryServices.DirectoryEntry
"LDAP://CN=BusinessObjects,OU=Internet Access Control
Groups,OU=Groups,DC=ColasUK,DC=int" | select-object -expandproperty member

# Reads through array returning members details
foreach ($x in $grp) {New-Object DirectoryServices.DirectoryEntry
"LDAP://$x" | select-object sAMAccountName,displayname } | sort

#############


Cheers

 
Old 03-09-2007   #2 (permalink)
n3llyb0y
Guest
 
Posts: n/a

Re: Sorting output from foreach

The sort function is happening outside of the itteration so there is no
group of objects to actually sort.

Try this on for size:

$grp = new-object DirectoryServices.DirectoryEntry
"LDAP://CN=BusinessObjects,OU=Internet Access Control
Groups,OU=Groups,DC=ColasUK,DC=int"

foreach ($member in ($grp.member | sort)) {write $(new-object
DirectoryServices.DirectoryEntry "LDAP://$member" | select-object
sAMAccountName,displayname)}

Basically, this sorts the data based on the cn before the attributes
are retrieved. Should be okay as the SamAccountName should be the cn
(at least it is in my environment)

Cheers,
Neil

On 2007-03-09 11:15:00 +0000, SteveHodd <steveHodd@hotmail.com> said:

> Hi,
>
> I'm quite new powershell.
> I've written a script that will list the SamAccount and DisplayName of
> all members of an AD group. I want to sort the output from the foreach
> statement by the SamAccount field, but I can't get it to work. Does
> anyone have any ideas or a better way to do this?
>
> #############
> # this line returns members FQDN as an array
> $grp = New-Object DirectoryServices.DirectoryEntry
> "LDAP://CN=BusinessObjects,OU=Internet Access Control
> Groups,OU=Groups,DC=ColasUK,DC=int" | select-object -expandproperty
> member
>
> # Reads through array returning members details foreach ($x in $grp)
> {New-Object DirectoryServices.DirectoryEntry "LDAP://$x" |
> select-object sAMAccountName,displayname } | sort
>
> #############
>
>
> Cheers



 
Old 03-09-2007   #3 (permalink)
SteveHodd
Guest
 
Posts: n/a

Re: Sorting output from foreach

Thanks, but the cn in my environment is "surname firstname", so the sort
still does not work.


"n3llyb0y" wrote:

> The sort function is happening outside of the itteration so there is no
> group of objects to actually sort.
>
> Try this on for size:
>
> $grp = new-object DirectoryServices.DirectoryEntry
> "LDAP://CN=BusinessObjects,OU=Internet Access Control
> Groups,OU=Groups,DC=ColasUK,DC=int"
>
> foreach ($member in ($grp.member | sort)) {write $(new-object
> DirectoryServices.DirectoryEntry "LDAP://$member" | select-object
> sAMAccountName,displayname)}
>
> Basically, this sorts the data based on the cn before the attributes
> are retrieved. Should be okay as the SamAccountName should be the cn
> (at least it is in my environment)
>
> Cheers,
> Neil
>
> On 2007-03-09 11:15:00 +0000, SteveHodd <steveHodd@hotmail.com> said:
>
> > Hi,
> >
> > I'm quite new powershell.
> > I've written a script that will list the SamAccount and DisplayName of
> > all members of an AD group. I want to sort the output from the foreach
> > statement by the SamAccount field, but I can't get it to work. Does
> > anyone have any ideas or a better way to do this?
> >
> > #############
> > # this line returns members FQDN as an array
> > $grp = New-Object DirectoryServices.DirectoryEntry
> > "LDAP://CN=BusinessObjects,OU=Internet Access Control
> > Groups,OU=Groups,DC=ColasUK,DC=int" | select-object -expandproperty
> > member
> >
> > # Reads through array returning members details foreach ($x in $grp)
> > {New-Object DirectoryServices.DirectoryEntry "LDAP://$x" |
> > select-object sAMAccountName,displayname } | sort
> >
> > #############
> >
> >
> > Cheers

>
>
>

 
Old 03-09-2007   #4 (permalink)
n3llyb0y
Guest
 
Posts: n/a

Re: Sorting output from foreach

in that case you need a temporary store for your results before sorting them.

There are a number of ways to achieve that. I like the dictionary object

$diObj = new-object -comobject Scripting.Dictionary

$grp = new-object DirectoryServices.DirectoryEntry
"LDAP://CN=BusinessObjects,OU=Internet Access Control
Groups,OU=Groups,DC=ColasUK,DC=int"

foreach ($member in $grp.member) {
$myMember = new-object DirectoryServices.DirectoryEntry "LDAP://$member"
$diObj.add($myMember.sAMAccountName,$myMember.displayname)
}
$diObj.items() | sort
On 2007-03-09 12:43:10 +0000, SteveHodd <steveHodd@hotmail.com> said:

> Thanks, but the cn in my environment is "surname firstname", so the
> sort still does not work.
>
>
> "n3llyb0y" wrote:
>
>> The sort function is happening outside of the itteration so there is no
>> group of objects to actually sort.
>>
>> Try this on for size:
>>
>> $grp = new-object DirectoryServices.DirectoryEntry
>> "LDAP://CN=BusinessObjects,OU=Internet Access Control
>> Groups,OU=Groups,DC=ColasUK,DC=int"
>>
>> foreach ($member in ($grp.member | sort)) {write $(new-object
>> DirectoryServices.DirectoryEntry "LDAP://$member" | select-object
>> sAMAccountName,displayname)}
>>
>> Basically, this sorts the data based on the cn before the attributes
>> are retrieved. Should be okay as the SamAccountName should be the cn
>> (at least it is in my environment)
>>
>> Cheers,
>> Neil
>>
>> On 2007-03-09 11:15:00 +0000, SteveHodd <steveHodd@hotmail.com> said:
>>
>>> Hi,
>>>
>>> I'm quite new powershell.
>>> I've written a script that will list the SamAccount and DisplayName of
>>> all members of an AD group. I want to sort the output from the foreach
>>> statement by the SamAccount field, but I can't get it to work. Does
>>> anyone have any ideas or a better way to do this?
>>>
>>> #############
>>> # this line returns members FQDN as an array
>>> $grp = New-Object DirectoryServices.DirectoryEntry
>>> "LDAP://CN=BusinessObjects,OU=Internet Access Control
>>> Groups,OU=Groups,DC=ColasUK,DC=int" | select-object -expandproperty
>>> member
>>>
>>> # Reads through array returning members details foreach ($x in $grp)
>>> {New-Object DirectoryServices.DirectoryEntry "LDAP://$x" |
>>> select-object sAMAccountName,displayname } | sort
>>>
>>> #############
>>>
>>>
>>> Cheers



 
Old 03-09-2007   #5 (permalink)
n3llyb0y
Guest
 
Posts: n/a

Re: Sorting output from foreach

Actually, the output of the dictionary object needs something else

foreach ($key in ($diObj | sort)){
write-host $key $diObj.Item($key)
}

cheers,
n

On 2007-03-09 13:18:20 +0000, n3llyb0y <n3llyb0y@aol.com> said:

> in that case you need a temporary store for your results before sorting them.
>
> There are a number of ways to achieve that. I like the dictionary object
>
> $diObj = new-object -comobject Scripting.Dictionary
>
> $grp = new-object DirectoryServices.DirectoryEntry
> "LDAP://CN=BusinessObjects,OU=Internet Access Control
> Groups,OU=Groups,DC=ColasUK,DC=int"
>
> foreach ($member in $grp.member) {
> $myMember = new-object DirectoryServices.DirectoryEntry "LDAP://$member"
> $diObj.add($myMember.sAMAccountName,$myMember.displayname)
> }
> $diObj.items() | sort
> On 2007-03-09 12:43:10 +0000, SteveHodd <steveHodd@hotmail.com> said:
>
>> Thanks, but the cn in my environment is "surname firstname", so the
>> sort still does not work.
>>
>>
>> "n3llyb0y" wrote:
>>
>>> The sort function is happening outside of the itteration so there is no
>>> group of objects to actually sort.
>>>
>>> Try this on for size:
>>>
>>> $grp = new-object DirectoryServices.DirectoryEntry
>>> "LDAP://CN=BusinessObjects,OU=Internet Access Control
>>> Groups,OU=Groups,DC=ColasUK,DC=int"
>>>
>>> foreach ($member in ($grp.member | sort)) {write $(new-object
>>> DirectoryServices.DirectoryEntry "LDAP://$member" | select-object
>>> sAMAccountName,displayname)}
>>>
>>> Basically, this sorts the data based on the cn before the attributes
>>> are retrieved. Should be okay as the SamAccountName should be the cn
>>> (at least it is in my environment)
>>>
>>> Cheers,
>>> Neil
>>>
>>> On 2007-03-09 11:15:00 +0000, SteveHodd <steveHodd@hotmail.com> said:
>>>
>>>> Hi,
>>>>
>>>> I'm quite new powershell.
>>>> I've written a script that will list the SamAccount and DisplayName of
>>>> all members of an AD group. I want to sort the output from the foreach
>>>> statement by the SamAccount field, but I can't get it to work. Does
>>>> anyone have any ideas or a better way to do this?
>>>>
>>>> #############
>>>> # this line returns members FQDN as an array
>>>> $grp = New-Object DirectoryServices.DirectoryEntry
>>>> "LDAP://CN=BusinessObjects,OU=Internet Access Control
>>>> Groups,OU=Groups,DC=ColasUK,DC=int" | select-object -expandproperty
>>>> member
>>>>
>>>> # Reads through array returning members details foreach ($x in $grp)
>>>> {New-Object DirectoryServices.DirectoryEntry "LDAP://$x" |
>>>> select-object sAMAccountName,displayname } | sort
>>>>
>>>> #############
>>>>
>>>>
>>>> Cheers



 
Old 03-09-2007   #6 (permalink)
SteveHodd
Guest
 
Posts: n/a

Re: Sorting output from foreach

That works a treat.

Thanks for your help
Steve

"n3llyb0y" wrote:

> Actually, the output of the dictionary object needs something else
>
> foreach ($key in ($diObj | sort)){
> write-host $key $diObj.Item($key)
> }
>
> cheers,
> n
>
> On 2007-03-09 13:18:20 +0000, n3llyb0y <n3llyb0y@aol.com> said:
>
> > in that case you need a temporary store for your results before sorting them.
> >
> > There are a number of ways to achieve that. I like the dictionary object
> >
> > $diObj = new-object -comobject Scripting.Dictionary
> >
> > $grp = new-object DirectoryServices.DirectoryEntry
> > "LDAP://CN=BusinessObjects,OU=Internet Access Control
> > Groups,OU=Groups,DC=ColasUK,DC=int"
> >
> > foreach ($member in $grp.member) {
> > $myMember = new-object DirectoryServices.DirectoryEntry "LDAP://$member"
> > $diObj.add($myMember.sAMAccountName,$myMember.displayname)
> > }
> > $diObj.items() | sort
> > On 2007-03-09 12:43:10 +0000, SteveHodd <steveHodd@hotmail.com> said:
> >
> >> Thanks, but the cn in my environment is "surname firstname", so the
> >> sort still does not work.
> >>
> >>
> >> "n3llyb0y" wrote:
> >>
> >>> The sort function is happening outside of the itteration so there is no
> >>> group of objects to actually sort.
> >>>
> >>> Try this on for size:
> >>>
> >>> $grp = new-object DirectoryServices.DirectoryEntry
> >>> "LDAP://CN=BusinessObjects,OU=Internet Access Control
> >>> Groups,OU=Groups,DC=ColasUK,DC=int"
> >>>
> >>> foreach ($member in ($grp.member | sort)) {write $(new-object
> >>> DirectoryServices.DirectoryEntry "LDAP://$member" | select-object
> >>> sAMAccountName,displayname)}
> >>>
> >>> Basically, this sorts the data based on the cn before the attributes
> >>> are retrieved. Should be okay as the SamAccountName should be the cn
> >>> (at least it is in my environment)
> >>>
> >>> Cheers,
> >>> Neil
> >>>
> >>> On 2007-03-09 11:15:00 +0000, SteveHodd <steveHodd@hotmail.com> said:
> >>>
> >>>> Hi,
> >>>>
> >>>> I'm quite new powershell.
> >>>> I've written a script that will list the SamAccount and DisplayName of
> >>>> all members of an AD group. I want to sort the output from the foreach
> >>>> statement by the SamAccount field, but I can't get it to work. Does
> >>>> anyone have any ideas or a better way to do this?
> >>>>
> >>>> #############
> >>>> # this line returns members FQDN as an array
> >>>> $grp = New-Object DirectoryServices.DirectoryEntry
> >>>> "LDAP://CN=BusinessObjects,OU=Internet Access Control
> >>>> Groups,OU=Groups,DC=ColasUK,DC=int" | select-object -expandproperty
> >>>> member
> >>>>
> >>>> # Reads through array returning members details foreach ($x in $grp)
> >>>> {New-Object DirectoryServices.DirectoryEntry "LDAP://$x" |
> >>>> select-object sAMAccountName,displayname } | sort
> >>>>
> >>>> #############
> >>>>
> >>>>
> >>>> Cheers

>
>
>

 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting output in Ascending rather than Descending j0seg PowerShell 1 01-28-2008 03:03 PM
Re: Empty output oddity in conjunction with foreach statement Jacques Barathon [MS] PowerShell 1 03-28-2007 12:17 AM
Re: Empty output oddity in conjunction with foreach statement Rob Campbell PowerShell 0 03-27-2007 08:05 PM
Output of Powershell ForEach Loop Morkcallorson PowerShell 1 01-15-2007 04:29 PM
sorting the help output Luke PowerShell 2 12-06-2006 02:04 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49