从带有参数的invoke-command返回数据调用函数

时间:2019-10-16 14:51:18

标签: function powershell parameters invoke-command

很抱歉。.
尝试修改当前使用robocopy报告文件夹路径大小,子文件夹数和大小的脚本。当前,此脚本大约需要24小时运行,因为它逐个进入每个文件夹。我正在尝试实现invoke-command来设置作业,以便它将至少运行10个robocopy实例,以期大大减少运行时间。

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

Invoke-Command:无法使用指定的命名参数来解析参数集。 错误:+调用命令-ScriptBlock {参数($ item,$ Filter,$ params)$ ...
错误:+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
错误:+ CategoryInfo:InvalidArgument:(:) [Invoke-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

0 个答案:

没有答案