Solved

Trying to group data in a report

Posted on 2014-03-01
3
237 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
A procedure for exporting installed hotfix details of remote computers using powershell
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

920 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

16 Experts available now in Live!

Get 1:1 Help Now