Powershell: Script assistance

Hello,

I have a csv file with 2 columns, Column A is server name, and column B is log size. I have to add up all the values for server A, and the output should be Server A, 4gb , and Server B, 2.4 ((based on the following table). I have a csv file with about 5000 lines and 300 servers.

Server A-10-28      2gb
Server A-10-27      1gb
Server A-10-26      1gb
Server B-10-28      0.4gb
Server B -10-27      2gb

Could you please assist with a script. Really appreciate your assistance.
Parity123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
A few questions for you.
1. is the date format all ways the same?
2. Do the servers follow a naming convention?
3. Is there any non consistent formatting? (like sometimes there's spaces, sometimes not, sometimes a hyphen before the date, sometimes not, etc)
Parity123Author Commented:
1) ServerA-10-29-2015-11-03-21-AM.zip
2) No
3) Consistent
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Assuming we do not have a dash in the server name:
Import-CSV servers.csv -header file, used |
  % { @{ server = $_.file.Split('-')[0]; used = $_.used -replace 'gb'} } |
 Group-Object server |
 % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group | Measure-Object -Sum size | Select -Expand Sum } }

Open in new window

Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

Parity123Author Commented:
I am getting the following error, I have a csv with 2 columns, Name and Size

Measure-Object : Property "size" cannot be found in any object(s) input.
At C:\GetServersize.ps1:4 char:75
+  % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group |
Measur ...
+
~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumen
   tException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerSh
   ell.Commands.MeasureObjectCommand
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry, left-over from prior test code.
Import-CSV servers.csv -header file, used |
  % { @{ server = $_.file.Split('-')[0]; used = $_.size -replace 'gb'} } |
 Group-Object server |
 % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group | Measure-Object -Sum size | Select -Expand Sum } }

Open in new window

Parity123Author Commented:
I am getting the following error:

Measure-Object : Property "size" cannot be found in any object(s) input.
At c:\servers.ps1:4 char:75
+  % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group |
Measur ...
+
~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumen
   tException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerSh
   ell.Commands.MeasureObjectCommand

server                                  size
------                                  ----
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
[censored] I've changed the wrong part! And since you have column headers Name and Size in the CSV, we will use those.
Import-CSV servers.csv |
  % { @{ server = $_.Name.Split('-')[0]; size = $_.size -replace 'gb'} } |
 Group-Object server |
 % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group | Measure-Object -Sum size | Select -Expand Sum } 

Open in new window

Note: the Name in the last line is intentional and correct, it refers to the fixed property generated by Group-Object containing the group name.
Parity123Author Commented:
still the same error

Measure-Object : Property "size" cannot be found in any object(s) input.
At C:\servers.ps1:4 char:75
+  % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group |
Measur ...
+
~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumen
   tException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerSh
   ell.Commands.MeasureObjectCommand

server                                  size
------                                  ----
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I really have to apologize. This is all originating from me not copying the correct test code in the first place :/.
Hopefully this is the last try needed:
Import-CSV servers.csv |
  % { New-Object PsObject -Property @{ server = $_.Name.Split('-')[0]; size = $_.size -replace 'gb'} } |
  Group-Object server |
  % { New-Object PsObject -Property @{ server = $_.Name; size = $_.Group | Measure-Object -Sum size | Select -Expand Sum } 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.