0

为长度道歉..
尝试修改当前使用 robocopy 报告文件夹路径大小、子文件夹计数和大小的脚本。该脚本当前需要大约 24 小时才能运行,因为它会一个一个地运行到每个文件夹。我正在尝试实现调用命令来设置作业,以便它可以运行至少 10 个 robocopy 实例,以期大幅减少运行时间。

在给定多种资源的情况下,我尝试了多种差异来设置参数、调用函数并返回结果,但均未成功。当前代码(如下)出现错误;

Invoke-Command:无法使用指定的命名参数解析参数集。错误:+ Invoke-Command -ScriptBlock { param ($item,$Filter,$params) $ ...
错误:+ ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
错误:+ CategoryInfo : InvalidArgument: (:) [调用-Command],ParameterBindingException
错误:+ FullyQualifiedErrorId:AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand

    Function ListFolder ($Folder, $FilterSet, $Roboparam)
    {
        write-host $Folder
        Try
        {
            $Folder = (Resolve-Path -LiteralPath $Folder -ErrorAction Stop).ProviderPath
            If (-Not (Test-Path -LiteralPath $Folder -Type Container -ErrorAction Stop))
            {Write-Warning ("{0} is not a directory and will be skipped" -f $Folder)
                Return
            }

            $Script = robocopy $Folder NULL $FilterSet $FRoboparam
            $exit_code = $LASTEXITCODE

            16, 8, 4, 2, 1 | % {
                Switch ($exit_code -band $_)
                {
                    16  { $exit_reason = "Usage error or insufficient access privileges" }
                    8   { $exit_reason = "Retry limit exceeded" }
                    4   { $exit_reason = "Some Mismatched files or directories were detected" }
                    2   { $exit_reason = "Some Extra files or directories were detected. No files were copied" }
                    1   { $exit_reason = " " }
                }
            }
            If ($exit_code -eq 0)
            {$exit_reason = 'No Change'}

            If ($Script[-6] -match $dirPattern)
            {$Dir = $matches.Dir}
            Else
            {$Dir = 0}

            If ($Script[-5] -match $countPattern)
            {$Count = $matches.Count}
            Else
            {$Count = 0}

            If ($Count -gt 0)
            {If ($Script[-4] -match $sizePattern)
                {$FSize = $matches.Size}}
            Else
            {$FSize = 0}

            $Script:TotSize += $FSize
            $Script:Report += New-Object PSObject -Property @{
                'Folder Name' = $Folder
                Files         = "{0:N0}" -f [int]$Count
                Folders       = "{0:N0}" -f [int]$Dir
                Size          = "{0:N0}" -f [long]$FSize
                Comment       = $exit_reason}

            Clear-Variable -Name Dir
            Clear-Variable -Name Count
            Clear-Variable -Name FSize
            If ($exit_reason) { Clear-Variable -Name exit_reason }}
        Catch
        {$Script:Report += New-Object PSObject -Property @{
                'Folder Name' = $Folder
                Files         = "{0:N0}" -f [int]$Count
                Folders       = "{0:N0}" -f [int]$Dir
                Size          = [long]$FSize
                Comment       = [string]$_.Exception.Message}
            Continue
        }

        Return #$Script
    }
    }

$params = New-Object System.Collections.Arraylist
$params.AddRange(@("/L","/S","/NJH","/BYTES","/FP","/NFL","/NC","/NDL","/TS","/XJ","/R:0","/W:0"))
$dirPattern = "^\s{4}Dirs\s:\s+(?<Dir>\d+).*"
$countPattern = "^\s{3}Files\s:\s+(?<Count>\d+).*"
$sizePattern = "^\s{3}Bytes\s:\s+(?<Size>\d+(?:\.?\d+)).*"

If ($PSBoundParameters['Force']) {$FileDate = 10}

If ($FileDate -lt 6){Exit}
Else
{$Paths = Get-Content $InputFile
    ForEach ($item in $Paths)
    {
        $MaxThreads = 10
        While (@(Get-Job | where { $_.State -eq "Running" }).Count -ge $MaxThreads)
            {
            Write-Host "Waiting for open thread...($MaxThreads Maximum)"
            Start-Sleep -Seconds 3
            }
        Invoke-Command -Computer . -ScriptBlock { param ($item,$Filter,$params) ${function:ListFolder} } -ArgumentList $item, $Filter, $params -AsJob
    }
}

总而言之,我有一个 $Report 集合来显示文件夹名称、文件数、文件夹数、字节大小以及向 Excel 显示任何错误的注释

$Report | Sort-Object {[long]$_.Size} -descending | Select 'Folder Name', Files, Folders, Size, Comment | Export-Csv -Path $ReportPath\$(Get-Date -uformat "%Y_%m_%d")-FileSizes.csv -Encoding ascii -NoTypeInformation
4

0 回答 0