Powershell calculated property not working when integrated with the ForEach loop

Hi Folks,

I need some help in tweaking or adjusting the below Powershell script to gather and collect the Top 32 biggest files and its total size from multiple different large file servers.

The Server name is populated from $.ComputerName variable.
The Directory path for each of the servers is different as per $
.ContentPath variable.

I have tested the below in each Remote Desktop session to the server, and opening Powershell ISE, both script returns the numbers correctly, although waiting for 4-5 hours.

Script 1 Using GCI, throws thousands of error on the screen due to 260 characters limit:

$BufferSize = 100
$FileSizes = New-Object System.Collections.ArrayList
Get-ChildItem 'S:\Global Data\DFS Share' -Force -Recurse -File | ForEach-Object {
    $null = $FileSizes.Add($_.Length)
    if ($FileSizes.Count -gt $BufferSize) {
        $FileSizes.Sort()
        $FileSizes.RemoveRange(0, ($BufferSize - 32))
    }
}
[Math]::Round((($FileSizes[0..31] | Measure-Object -Sum).Sum / 1GB), 2)

Script 2 Using Robocopy:

[Math]::Round(((robocopy /L /E /NDL /NJH /NJS /NP /NODCOPY /BYTES 'C:\Shares\DFS Data' 'NoDestination' | ForEach-Object {
    [int64]([regex]'(?i)New File\s*(\d+)').Match($_).Groups[1].Value
} | Sort-Object -Descending | Select-Object -First 32 | Measure-Object -Sum).Sum / 1GB), 2)

Combined script to check which method works consistently:

Try {
    $results = Get-DfsrMembership | ForEach-Object {
        $driveLetter = ($_.ContentPath -split ':')[0]
        $BufferSize = 100
        $FileSizes = New-Object System.Collections.ArrayList


        $wmiDisk = Get-WmiObject -ComputerName $_.ComputerName -Query "Select Capacity, FreeSpace From Win32_Volume Where (DriveLetter = '$($driveLetter):')"
        $_ | Select-Object -Property `
        @{ n = 'Server - IP'; e = { "$($_.ComputerName) [$((Resolve-DnsName -Name $_.ComputerName -Type A).IPAddress)]" } },
        @{ n = 'Drive Letter with data'; e = { $driveLetter } },
        @{ n = 'FreeSpaceGB'; e = { [math]::Round(($wmiDisk.FreeSpace / 1GB), 2) } },
        @{ n = 'Free Disk Percent'; e = { " $([Math]::Round(($wmiDisk.FreeSpace / $wmiDisk.Capacity) * 100, 2)) %" } },
        @{ n = 'Staging Path Quota GB'; e = { ($_.StagingPathQuotaInMB / 1000) } },
        @{ n = 'DFS Service Status'; e = { (Get-Service -Name DFS* -ComputerName $_.ComputerName | ForEach-Object { "$($_.Name): $($_.Status)" }) -join '; ' } },
        @{ n = 'Top 32 Largest Files Size - Robocopy'; e = { 
                (robocopy /L /E /NDL /NJH /NJS /NP /NODCOPY /BYTES $_.ContentPath 'NoDestination' | ForEach-Object {
                        [int64]([regex]'(?i)New File\s*(\d+)').Match($_).Groups[1].Value 
                    } | Sort-Object -Descending | Select-Object -First 32 | Measure-Object -Sum).Sum / 1GB }
        },
        @{  n = 'Top 32 Largest Files Size - GCI'
            e = { Get-ChildItem $_.ContentPath -Force -Recurse -File | ForEach-Object {
                    $null = $FileSizes.Add($_.Length)
                    if ($FileSizes.Count -gt $BufferSize) {
                        $FileSizes.Sort()
                        $FileSizes.RemoveRange(0, ($BufferSize - 32))
                    }
                }
                [Math]::Round((($FileSizes[0..31] | Measure-Object -Sum).Sum / 1GB), 2)
            }
        },
        GroupName,
        ContentPath,
        State
    }
    $results | Sort-Object 'Top 32 Largest Files Size - GCI'
    $results | Sort-Object 'Top 32 Largest Files Size - Robocopy'
}
Catch {
    $_ | Write-Error
}



<u>Problem</u>: both of the column Top 32 Largest Files Size - GCI and Top 32 Largest Files Size - Robocopy only shows the first two servers, while the rest is just 0 and empty. No error throws out, apart from the 260 characters limit.

However, when using the below-combined script as Calculated property, the script just returning the first two servers, not 10+ DFS File servers I have globally.
each of the servers has about 2-3 million files, hence I am using the Integer64 [long].

Any help would be greatly appreciated.

Thank you in advance.

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

https://stackoverflow.com/questions/63705878/powershell-calling-robocopy-to-get-top32-largest-files-total-size-not-working

Thanks

[quote quote=254354]When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you make their work twice or more.

https://stackoverflow.com/questions/63705878/powershell-calling-robocopy-to-get-top32-largest-files-total-size-not-working

Thanks

[/quote]

That’s great, thank you :slight_smile: