Solved

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

Posted on 2013-02-07
3
290 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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