Solved

Add row number to data after sort-object has been applied

Posted on 2013-02-07
3
293 Views
Last Modified: 2013-02-13
I have a load of data which I am applying a Sort-Object to, is there a way I can add a row number to the data after the sort. I will be using this to calculate a percentage of the overall work complete...
0
Comment
Question by:Blowfelt82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:Blowfelt82
ID: 38863590
Infact it would also be useful if I could add a column to the data with average run rates etc based on other columns data... If someone could show me roughly how I could acheive that I should be able to work the rest out... Code so far (coutesy of segwig is...)

$total= 5785
'MAC1,'MAC2','MAC3','MAC4' | %{
$path = "\\$_\Z$\TEST\ROUTE"
$mach = $_
Get-ChildItem $path -include ('initialised.txt') -recurse | Select-Object CreationTime, LastWriteTime, @{Name="Machine";Expression={$mach}} , @{Name="Harness";Expression={($_.Directory -Replace ([regex]::Escape("\\$_\Z$\TEST\ROUTE"),""))}}, @{Name="TotalHarnesses";Expression={$total_harnesses}}, @{Name="RunTime";Expression={"{0:N0}" -f ($_.LastWriteTime - $_.CreationTime).TotalMinutes}}| Where-Object {$_.RunTime -gt 0} | Sort-Object CreationTime 
} # | Export-Csv "C:\MachineLoad.txt"

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 38866715
I'm not sure how the whole "percentage complete" thing is supposed to work, but this should work for adding a row number.  Add the following after the sort to pipe it's output.
| ForEach-Object { $i++; $_ | Add-Member -type noteproperty -name Index -value $i; $_}

Open in new window

You wouldn't be able to then just pipe that to Export-CSV though.  You'd have to save all this data to another variable.  You could then work with that variable to create a new calculated property for average run rates and then output the data.  If I'm understanding what your ouput will be, each row will have a runtime, it couldn't then have an average as well.  What's being averaged?
0
 

Author Closing Comment

by:Blowfelt82
ID: 38884789
Sorry for the late reply, perfect answer!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question