tags:

views:

62

answers:

1

Hi there,

I'm looking to calculate the total size of all shared folders (except admin shares) on a number of different servers (consolidating all accessed files to a NAS box for easier backup / restore) but am having a bit of trouble finding a solution.

I'm certain this could be done in powershell but I just can't find the right information to get me going, I can currently spit out a list of all shares on the servers but am not sure where to go from here:

$servers =@( "server1", "server2")

foreach($server in $servers) { get-WmiObject Win32_Share -computerName $server -filter "Type = 0" }

A: 

I would try to use Get-ChildItem to list the files and Measure-Object to count the sizes

$servers = @("server1", "server2")
$sizes = @()
foreach($server in $servers) {
  write-host "Server: $server"
  $serverSizes = @(gwmi -class Win32_Share -ComputerName $server -filter "Type = 0" | 
    % { 
      write-host " share: $($_.Name)"
      $s = gci \\$server\$($_.Name) -recurse -force | Measure-Object -Property length -Sum
      New-Object PSObject -property @{Name=$_.Name; Server=$server; TotalSize=$s.Sum } 
    })
  if ($serverSizes) {
     $totalServerSize = $serverSizes | Measure-Object -Property TotalSize -Sum
     $serverSizes += New-Object PSObject -property @{Name="__Total__"; Server=$server; TotalSize=$totalServerSize.Sum } 
     $sizes += $serverSizes
  }
}

Then you can e.g. select the total sizes like this:

$sizes | 
  ? { $_.Name -eq '__Total__' } | 
  Select-Object Server,@{L='Size in MB'; E={$_.TotalSize/1mb}},@{L='Size in GB'; E={$_.TotalSize/1gb}}
stej
Thanks stej, that works great only problem is I can't tell what the measure size is of the total output (MB, GB?)
bEUY
Edited code and added Select-Object that calculates the sizes in MB and GB.
stej
Thanks stej that works wonders! As a side effect this is also helping me find out what folders don't have correct permissions to be accessed by Administrative Services (backup jobs etc).
bEUY