locked
Sum Columns using Powershell RRS feed

  • Question

  • I have written the following PowerShell script for getting disk space information for 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 from $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 -NoTypeInformation
    One additional requirement here is to get the sum of the columns for Capacity, Size and Freespace for each server. I tried using Measure-Object but no success. No values are getting outputted here. Just blank. Please look into this and kindly assist. Only Sum output is coming blank, rest everything is fine.

    Monday, March 30, 2015 9:14 PM

Answers

  • Hi Abhinav,

    Please try to add Sum column to the output, which will sum up the "Capacity(GB)" in the 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 from $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 is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang

    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Edited by AnnaWY Tuesday, March 31, 2015 11:40 AM
    • Proposed as answer by AnnaWY Tuesday, April 7, 2015 11:38 AM
    • Marked as answer by AnnaWY Friday, April 10, 2015 2:06 AM
    Tuesday, March 31, 2015 11:39 AM

All replies

  • Well this is PowerShell. It is not your Grandmothers old Apple 2e.

    Get-Content servers.txt |
        ForEach-Object{
            Get-WmiObject Win32_Volume -ComputerName $_
        } |
        select PSComputerName, Name, Capacity, FreeSpace, label |
        Measure-Object -Sum capacity,Freespace
    

    Yes - grandmothers are allowed to be geeks too.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Tuesday, March 31, 2015 5:01 AM
    Monday, March 30, 2015 11:20 PM
  • Clearly, there are more geeks playing with grandmother's apple 2e than the post above wants us to think.

    Let me enumerate the reasons that led me to such conclusion:

    1- Only a geek playing with grandmother's apple 2e would suggest someone to use Get-WmiObject cmdlet nowadays - the PSTeam recommend not using it.

    Any experienced PowerShell scripter would not do this.

    2- Even if the geek playing with grandmother's apple 2e insists in using Get-WmiObject cmdlet, there's no reason to use the ForEach-Object, because the -ComputerName takes a <string[]>.

    Any experienced PowerShell scripter would not do this.

    3- The geek playing with grandmother's apple 2e selects PSComputerName and Label (and 2 others more) and pipe those properties to Measure-Object, that does not use them - conclusion: those 2 properties are gnored, and make no part of the output.

    Definitely, any experienced PowerShell scripter would not do this.

    Tuesday, March 31, 2015 5:37 AM
  • Hi Abhinav,

    Please try to add Sum column to the output, which will sum up the "Capacity(GB)" in the 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 from $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 is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang

    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Edited by AnnaWY Tuesday, March 31, 2015 11:40 AM
    • Proposed as answer by AnnaWY Tuesday, April 7, 2015 11:38 AM
    • Marked as answer by AnnaWY Friday, April 10, 2015 2:06 AM
    Tuesday, March 31, 2015 11:39 AM