?
Solved

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

Posted on 2013-02-07
3
Medium Priority
?
296 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 2000 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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

771 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