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

sar*_*dok 4 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
Run Code Online (Sandbox Code Playgroud)

完整的复制功能在这里描述: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
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

CB.*_*CB. 14

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

Register-PSSessionConfiguration -Name DataNoLimits #or the name you like.
Run Code Online (Sandbox Code Playgroud)

然后配置你想要的参数(在这种情况下,MaximumReceivedDataSizePerCommandMBMaximumReceivedObjectSizeMB):

Set-PSSessionConfiguration -Name DataNoLimits `
-MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500
Run Code Online (Sandbox Code Playgroud)

然后根据PSSessionConfiguration需要创建新会话:

$Session = New-PSSession -ComputerName MyRemoteComp -ConfigurationName DataNoLimits
Run Code Online (Sandbox Code Playgroud)

在您的本地计算机中.

通过这种方式使用posh.org的Send-File,我复制了一个大小约为80MB的文件.更大的尺寸让我失去了存在的例外.

更多关于这里.