Sum Columns using Powershell
i have written following powershell script getting disk space information servers in our environment.
$servers = get-content e:\poc.txt $array = @() foreach($server in $servers){ $sysinfo = get-wmiobject win32_volume -computername $server for($i = 0;$i -lt $sysinfo.count; $i++){ $sname = $sysinfo[$i].systemname $servername = $server $label = $sysinfo[$i].label if(($label) -and (!($label.contains("filler")))){ write-host "processing $label $server" $name = $sysinfo[$i].name $capacity = [math]::round(($sysinfo[$i].capacity/1gb),2) $fspace = [math]::round(($sysinfo[$i].freespace/1gb),2) $sused = [math]::round((($sysinfo[$i].capacity - $sysinfo[$i].freespace)/1gb),2) $fspacepercent = [math]::round((($sysinfo[$i].freespace*100)/$sysinfo[$i].capacity),2) $obj = new-object psobject $obj | add-member -membertype noteproperty -name "systemname" -value $sname $obj | add-member -membertype noteproperty -name "servername" -value $server $obj | add-member -membertype noteproperty -name "label" -value $label $obj | add-member -membertype noteproperty -name "name" -value $name $obj | add-member -membertype noteproperty -name "capacity(gb)" -value $capacity $obj | add-member -membertype noteproperty -name "freespace(gb)" -value $fspace $obj | add-member -membertype noteproperty -name "used(gb)" -value $sused $obj | add-member -membertype noteproperty -name "freespace%" -value $fspacepercent $array += $obj } } $array += write-output " " $totalsize = ($array | measure-object 'capacity(gb)' -sum).sum $array += $totalsize $array += write-output " " } $filename = "e:\volumereport.csv" $array | export-csv $filename -notypeinformationone additional requirement here sum of columns capacity, size , freespace each server. tried using measure-object no success. no values getting outputted here. blank. please , kindly assist. sum output coming blank, rest fine.
hi abhinav,
please try add sum column output, will sum up the "capacity(gb)" in end of each server:
$servers = get-content e:\poc.txt $filename = "e:\volumereport.csv" $result = @() $array = @() foreach($server in $servers){ $sysinfo = get-wmiobject win32_volume -computername $server for($i = 0;$i -lt $sysinfo.count; $i++){ $sname = $sysinfo[$i].systemname $servername = $server $label = $sysinfo[$i].label if(($label) -and (!($label.contains("filler")))){ write-host "processing $label $server" $name = $sysinfo[$i].name $capacity = [math]::round(($sysinfo[$i].capacity/1gb),2) $fspace = [math]::round(($sysinfo[$i].freespace/1gb),2) $sused = [math]::round((($sysinfo[$i].capacity - $sysinfo[$i].freespace)/1gb),2) $fspacepercent = [math]::round((($sysinfo[$i].freespace*100)/$sysinfo[$i].capacity),2) $obj = new-object psobject $obj | add-member -membertype noteproperty -name "systemname" -value $sname $obj | add-member -membertype noteproperty -name "servername" -value $server $obj | add-member -membertype noteproperty -name "label" -value $label $obj | add-member -membertype noteproperty -name "name" -value $name $obj | add-member -membertype noteproperty -name "capacity(gb)" -value $capacity $obj | add-member -membertype noteproperty -name "freespace(gb)" -value $fspace $obj | add-member -membertype noteproperty -name "used(gb)" -value $sused $obj | add-member -membertype noteproperty -name "freespace%" -value $fspacepercent $obj | add-member -membertype noteproperty -name "sum" -value '' $array += $obj } } $array[$sysinfo.count-1].sum = ($array | measure-object 'capacity(gb)' -sum).sum $result += $array $array=@() } $result | export-csv $filename -notypeinformation
if there else regarding issue, please feel free post back.
best regards,
anna wang
technet community support
please remember mark replies answers if , unmark them if provide no help. if have feedback technet support, contact tnmff@microsoft.com
Windows Server > Windows PowerShell
Comments
Post a Comment