Getting the Directory Size on Remote Servers.

Use the following script:

 

$dataColl = @()#Makes an array, or a collection to hold all the object of the same fields.

foreach ($serverName in (get-content "c:\servers.txt"))

{
$path = "\\$serverName\f$\temp"
$dirSize = Get-ChildItem $path -recurse -force | select Length |Measure-Object -Sum -property length
$dirSize.sum = $dirSize.sum/1GB
$finalResult = "{0:N2} GB" -f $dirsize.sum
$dataObject = New-Object PSObject
Add-Member -inputObject $dataObject -memberType NoteProperty -name "ServerName" -value $serverName
Add-Member -inputObject $dataObject -memberType NoteProperty -name "Dir_Size" -value $finalResult
$dataColl += $dataObject
$dataObject
}

$dataColl | Out-GridView -Title "Remote Directory Scan Results"
$dataColl | Export-Csv -noTypeInformation -path "c:\temp.csv"

 

to workaround the error: Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

 

use the following script:

$dataColl = @()#Makes an array, or a collection to hold all the object of the same fields.

foreach ($serverName in (get-content "c:\servers.txt"))

{
$path = "\\$serverName\f$\data"
#$dirSize = Get-ChildItem $path -recurse -force | select Length |Measure-Object -Sum -property length
$dirSize = (robocopy.exe $path $env:Temp /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ")[2]
$dirsum = $dirSize/1GB
$finalResult = "{0:N2} GB" -f $dirsum
$dataObject = New-Object PSObject
Add-Member -inputObject $dataObject -memberType NoteProperty -name "ServerName" -value $serverName
Add-Member -inputObject $dataObject -memberType NoteProperty -name "Dir_Size" -value $finalResult
$dataColl += $dataObject
$dataObject
}

$dataColl | Out-GridView -Title "Remote Directory Scan Results"
$dataColl | Export-Csv -noTypeInformation -path "c:\temp.csv"

 

source: http://thescriptlad.com/2011/04/16/get-remote-dir-size/

Leave a Reply

Your email address will not be published. Required fields are marked *