Link to home
Start Free TrialLog in
Avatar of Parity123
Parity123Flag for United States of America

asked on

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.
Avatar of Jeremy Weisinger
Jeremy Weisinger

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)
Avatar of Parity123

ASKER

1) ServerA-10-29-2015-11-03-21-AM.zip
2) No
3) Consistent
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

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

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
------                                  ----
[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.
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
------                                  ----
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial