Powershell: Script assistance

Parity123
Parity123 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
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)

Author

Commented:
1) ServerA-10-29-2015-11-03-21-AM.zip
2) No
3) Consistent
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

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 Advisor
Top Expert 2015

Commented:
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

Author

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 Advisor
Top Expert 2015

Commented:
[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.

Author

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
------                                  ----
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial