Solved

Trying to group data in a report

Posted on 2014-03-01
3
236 Views
Last Modified: 2014-03-01
I have a csv file of servers and the amount of data each one uses. I'd like to create a report that contains groups of servers that have a combined total storage of 500GB or less. I'm virutalizing these servers and need to fit them onto 500GB datastores.  

The csv file looks like this:
Server,Storage
servr1,200gb
servr2,100gb
servr3,420gb
servr4,350gb
servr5,50gb

I'm trying to group them so that I would end up with output like this:
Server,Storage,group
servr1,200gb,group1
servr2,100gb,group1
servr3,420gb,group2
servr4,350gb,group3
servr5,50gb,group3

Is there a way in powershell  I could do a "do while" loop or maybe a way in excel that I could do this?
0
Comment
Question by:LN41
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39897849
This should work:
$input = "X:\your\path\servers.csv"
$output = "x:\your\path\servergroups.csv"
$maxStorage = 500

if (!(Test-Path $output)) {
    New-Item $output
}
echo "Server,Storage,group" > $output
$groupNo = 1
$groupStorage = 0
Import-Csv $input | % {
    $currServerStorage = $_.Storage.substring(0,$_.Storage.length - 2)
    if ($groupStorage + $currServerStorage -ge $maxStorage) {
        $groupNo ++
        $groupStorage = 0
    } 
    echo "$($_.Server),$($_.Storage),Group$groupNo" >> $output
    $groupStorage += $currServerStorage
}

Open in new window

HTH,
Dan
0
 
LVL 1

Author Closing Comment

by:LN41
ID: 39897937
That's perfect. Just tested and it's exactly what I needed. One thing though - when I run the script, I get a prompt that says "Type:" I hit enter and it fails, second time it runs and provides output file. Would you know what it's asking for for "Type"? Output type? If not, no biggy, this is a huge help either way.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39898186
Replace this:
New-Item $output
with
New-Item $output -type file
and it won't ask for the type anymore :)

Glad I could help!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now