Avatar of Parity123
Parity123
Flag 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.
Powershell

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
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)
Parity123

ASKER
1) ServerA-10-29-2015-11-03-21-AM.zip
2) No
3) Consistent
Qlemo

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Parity123

ASKER
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

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

Parity123

ASKER
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
------                                  ----
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Qlemo

[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.
Parity123

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.