远程命令中的最大数据大小

时间:2012-11-26 09:14:58

标签: powershell powershell-remoting

我的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会话对象。

是否需要在远程计算机或其他位置执行此配置?是什么导致了这个错误?

感谢。

2 个答案:

答案 0 :(得分:13)

您需要在远程计算机中创建一个新的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的Send-File我复制一个大小约为80MB的文件。 更大的尺寸让我失去了存在的例外。

有关此here.

的更多信息

答案 1 :(得分:0)

您可以查看this post关于后台智能传输服务(BITS)的信息。要获得更多帮助,您还可以查看MSDN documentation。本文中有以下注意事项:

  

使用BITS协议的优点:

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

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

希望有帮助。