Voici un petit script Powershell permettant de récupérer la taille d'un ou plusieurs share.
Ce script peut être appelé de deux manières :
- sans arguments : le script va chercher un fichier texte dans le répertoire d’exécution pour passer sur TOUS les répertoires indiqués dans le fichier (un partage par ligne) ; le but est donc de renseigner tous les partages pour lesquels on souhaite connaître la volumétrie dans le fichier. Le script retourne ensuite un fichier CSV :
- avec des arguments : en donnant en argument d’exécution du script le serveur et le partage, on récupère la taille dans la console directement :
param([string]$shareHost, [string]$shareName) function folderSize() { param($path) try { $byteSize = (Get-ChildItem -path "$path" -Recurse -ErrorAction SilentlyContinue | Measure-Object Length -sum).Sum if ($byteSize.ToString().Length -lt 4) { return "$byteSize B." } if ($byteSize.ToString().Length -ge 4 -and $byteSize.ToString().Length -lt 7) { $kbSize = [math]::Round($byteSize/1024,1) ; return "$kbSize KB." } if ($byteSize.ToString().Length -ge 7 -and $byteSize.ToString().Length -lt 10) { $mbSize = [math]::Round($byteSize/1048576,1) ; return "$mbSize MB." } if ($byteSize.ToString().Length -ge 10 -and $byteSize.ToString().Length -lt 13) { $gbSize = [math]::Round($byteSize/1073741824,1) ; return "$gbSize GB." } if ($byteSize.ToString().Length -ge 13) { $tbSize = [math]::Round($byteSize/1099511627776,2) ; return "$tbSize TB." } } catch { return "Share doesn't exist or is unreachable or you don't have the required privileges." } } Write-Host "Share size retriever Powershell script" Write-Host "======================================" Write-Host "Usage: .\win_foldersize.ps1 <SERVER> <SHARE>" Write-Host "If any parameter is missing, the script will look for a sharelist.txt file in the execution folder and will detect the size of all the folders inside the file." if ($shareHost -eq "" -or $shareName -eq "") { Write-Host "You didn't provide both parameters while running the script. I will now try to read directly the shares from sharelist.txt in the script folder." $today = Get-Date -f "yyMMdd" $CsvOutputFile = "share-size-$today.csv" $InputFile = "sharelist.txt" $CsvHeader = "Share,Size" Add-Content $CsvOutputFile $CsvHeader try { $ShareList = Get-Content $InputFile -ErrorAction Stop } catch { Write-Host "Unable to read sharelist.txt. Aborting." ; break } foreach ($Share in $ShareList) { $sizeDump = folderSize($Share) $Dump = "$Share,$sizeDump" Add-Content $CsvOutputFile $Dump } Write-Host "Done, output file : $CsvOutputFile" } else { Write-Host "Both parameters detected." Write-Host "Server specified: $shareHost" Write-Host "Share: $shareName" $shareString = "\\"+"$shareHost"+"\"+"$shareName" $displaySize = folderSize($shareString) Write-host $displaySize }