![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
| | #2 (permalink) |
| 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 |
| | #3 (permalink) |
| 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 > > > |
| | #4 (permalink) |
| 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 |
| | #5 (permalink) |
| 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 |
| | #6 (permalink) |
| 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 > > > |
| |
| |
![]() |
| 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 |