Powershell calling Robocopy to get top32 largest files total size not working

前端 未结 2 521
猫巷女王i
猫巷女王i 2021-01-29 01:45

The below script is a combination of DFS settings and also the Robocopy command to list the top 32 biggest files in the file servers.

I need to execute the below code aga

2条回答
  •  故里飘歌
    2021-01-29 02:37

    I agree with Steven in thinking your regex could be simpler and that you probably don't need the first ForEach-Object in your code.

    I cannot try this myself, but perhaps this is a faster alternative for you:

    @{ n = 'Top 32 Largest Files Size'; 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 }
    }
    

    This doesn't need the if to check each returned line from robocopy, because lines that do not match the regex will yield a value of 0


    Again, sorry I cannot test this myself, but perhaps it would be better to let the servers themselves do the heavy lifting of calculating the sizes. Especially because from your description, I understand that running the code RDP on each server individually works.

    In this case, you do need the first ForEach-Object loop.

    Please can you try

    $scriptBlock = {
        param ([string]$Path)
        (robocopy /L /E /NDL /NJH /NJS /NP /NODCOPY /BYTES $Path '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
    }
    
    $results = Get-DfsrMembership | ForEach-Object {
        Write-Host "Retrieving Top 32 Largest Files Size from server $($_.ComputerName).."
        # get the calculated size from the server
        # because of the large number of files, this may take some time..
        $size = Invoke-Command -ComputerName $_.ComputerName -ScriptBlock $scriptBlock -ArgumentList $_.ContentPath
        [PsCustomObject]@{
            'Server - IP'               = "$($_.ComputerName) [$((Resolve-DnsName -Name $_.ComputerName -Type A).IPAddress)]"
            'Staging Path Quota GB'     = ($_.StagingPathQuotaInMB / 1024)
            'Top 32 Largest Files Size' = $size
            'GroupName'                 = $_.GroupName
            'ContentPath'               = $_.ContentPath
            'State'                     = $_.State
        }
    }
    
    $results | Sort-Object 'Top 32 Largest Files Size'
    

    It is quite possible you need to add parameter -Credential to the Invoke-Command cmdlet

提交回复
热议问题