Home > Software engineering >  Single Folder Tree Size Report
Single Folder Tree Size Report

Time:02-03

I am trying to create a very specific Powershell report script that I can input a server list via CSV or TXT, and have it check a specific folder who's path exists on every server. Then output that information to a formatted CSV file with the server name, and the total size of that single folder tree.

I've been looking at the following thread: (Powershell folder size of folders without listing Subdirectories, as it seems to have the closest code examples of what I need to accomplish, but none of them are setup for a server report input and output.

The script shouldn't need any credentials as it will be running with elevated privileges. I don't think a function will be necessary as I plan to change the defined path as different situations come up.

I'm usually pretty good at piecing together different scripts I find. However I'm a bit stumped trying to find something that matches this exact function, so I'm not even sure where to start. I'm new to powershell and not very good at writing complex scripts by hand yet.

CodePudding user response:

servers.txt
 server1
 server2

there are two ways, one by smb, one using winRM

1 Powershell folder size of folders without listing Subdirectories

$servers = Get-Content "$PSScriptRoot\servers.txt"
$path = "c$\Distr"
$result = @()
foreach ($server in $servers){
  $fullpath = "\\"   $server   "\"   "$path"
  $fullpath
  $size = (Get-ChildItem -Recurse -Force $fullpath | Measure-Object -Property Length -Sum).sum 
  $array = "" | Select server, size
  $array.server = $server
  $array.size =  $size
  $result =$array
}

$result |Export-Csv -Delimiter ";" -Encoding UTF8 -Path "$PSScriptRoot\result.csv"

if the network connection is slow enough, it's better to use WinRM, but I don't have an example at hand right now.

CodePudding user response:

Thanks to rinat gadev for the starter code. Here's my final with adjustments that does exactly what I wanted.

    $servers = Get-Content "$PSScriptRoot\servers.txt"
$path = "c$\Distr"
$result = @()
foreach ($server in $servers){
  $fullpath = "\\"   $server   "\"   "$path"
  $fullpath
  $size = (Get-ChildItem -Recurse -Force $fullpath | Measure-Object -Property Length -Sum ).sum | ForEach-Object {[math]::Round($_/1GB,2)}
  $array = "" | Select-Object Server, Size
  $array.server = $server
  $array.size = $size 
  $result =$array
}

$result | Export-Csv -Path "$PSScriptRoot\result.csv"-NoTypeInformation -UseCulture
  • Related