4

我的 powershell 脚本使用以下代码将文件发送到自定义会话中的多个客户端(代码已缩短)

function DoCopyFile
{
    param(
    [Parameter(Mandatory=$true)] $RemoteHost,
    [Parameter(Mandatory=$true)] $SrcPath,
    [Parameter(Mandatory=$true)] $DstPath,
    [Parameter(Mandatory=$true)] $Session)
.
.
.               
    $Chunks | Invoke-Command -Session $Session -ScriptBlock { `
        param($Dest, $Length)

        $DestBytes = new-object byte[] $Length
        $Pos = 0
        foreach ($Chunk in $input) {
            [GC]::Collect()
            [Array]::Copy($Chunk, 0, $DestBytes, $Pos, $Chunk.Length)
            $Pos += $Chunk.Length
        }

        [IO.File]::WriteAllBytes($Dest, $DestBytes)
        [GC]::Collect()
    } -ArgumentList $DstPath, $SrcBytes.Length
.
.
.
}


$Pwd = ConvertTo-SecureString $Node.Auth.Password -asplaintext -force
$Cred = new-object -typename System.Management.Automation.PSCredential -ArgumentList ("{0}\{1}" -f $Name, $Node.Auth.Username),$Pwd
$Sopts = New-PSSessionOption -MaximumReceivedDataSizePerCommand 99000000
$Session = New-PSSession -ComputerName $Name -Credential $Cred -SessionOption $Sopts
DoCopyFile $Name ("{0}\{1}" -f $Node.Installer.ResourceDir, $Driver.Name) $Dest $Session

完整的复制功能在这里描述:http: //poshcode.org/2216

大于 52MB 的文件会出现问题。它失败并出现以下错误:

Sending data to a remote command failed with the following error message: The total data received from the remote
client exceeded allowed maximum. Allowed maximum is 52428800. For more information, see the
about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OperationStopped: (CLI-002:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : CLI-002

正如您在代码中看到的,我使用自定义的 ps 会话。当我将 MaximumReceivedDataSizePerCommand 设置为非常低的值(如 10kb)时,它会失败并显示一条消息,告诉最大值为 10kb,所以我假设 MaximumReceivedDataSizePerCommand 应用于 ps 会话对象。

是否需要在远程机器或其他地方进行此配置?是什么导致了这个错误?

谢谢。

4

2 回答 2

14

您需要PSSessionConfiguration在远程计算机中创建一个新的(这不使用默认的):

Register-PSSessionConfiguration -Name DataNoLimits #or the name you like.

然后配置您想要的参数(在本例中为MaximumReceivedDataSizePerCommandMBMaximumReceivedObjectSizeMB):

Set-PSSessionConfiguration -Name DataNoLimits `
-MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500

PSSessionConfiguration然后使用您需要的创建新会话:

$Session = New-PSSession -ComputerName MyRemoteComp -ConfigurationName DataNoLimits

在您的本地计算机中。

以这种方式使用来自 posh.org 的发送文件,我复制了一个 ~80MB 大小的文件。更大的尺寸返回我的内存不足异常。

更多关于这里。

于 2012-11-26T10:25:39.217 回答
0

您可以查看有关后台智能传输服务 (BITS)的帖子。如需更多帮助,您还可以查看MSDN 文档。从文章来看,有以下几点考虑:

使用 BITS 协议的优势:

  • BITS 是一种智能协议,能够控制使用的带宽而不影响其他网络应用程序的工作。BITS 只能使用空闲频段,并在传输过程中动态改变数据速率(如果其他应用程序增加网络使用)
  • 如果出现中断或计算机重新启动,BITS 任务将自动恢复
  • 文件可以在后台下载,用户不会注意到
  • 接收端和服务器端不需要部署 IIS 服务器

因此,BITS 是在慢速网络中传输大文件的首选协议。

希望有帮助。

于 2018-07-25T13:25:59.973 回答