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

标签 powershell powershell-remoting

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

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 session 。当我将 MaximumReceivedDataSizePerCommand 设置为非常低的值(如 10kb)时,它会失败并显示一条消息,告诉最大值为 10kb,因此我假设 MaximumReceivedDataSizePerCommand 应用于 ps session 对象。

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

谢谢。

最佳答案

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

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

然后配置您想要的参数(在本例中为 MaximumReceivedDataSizePerCommandMBMaximumReceivedObjectSizeMB ):
Set-PSSessionConfiguration -Name DataNoLimits `
-MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500

然后使用您需要的 PSSessionConfiguration 创建新 session :
$Session = New-PSSession -ComputerName MyRemoteComp -ConfigurationName DataNoLimits

在您的本地计算机中。

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

更多关于这个 here.

关于powershell - 远程命令中的最大数据大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561730/

相关文章:

powershell - 如何将 azure 中的捕获图像从一个订阅复制到另一个订阅

powershell - 如何在 PowerShell 中读取多行多字符串注册表项?

c# - 使用 c# 程序的参数调用远程 powershell 脚本

powershell - PowerShell强制参数取决于另一个参数

powershell - O365 PShell类似收件人搜索无法使用Where {$ _。RecipientAddress -like

windows - 在 PowerShell 中如何将文件从远程 PSsession 复制到另一台 Windows 服务器

powershell - 是否有可以启动另一台主机的 powershell 命令?

powershell - 使用 Connect-WSMan 会出现错误 : It cannot determine the content type of the HTTP response from the destination computer

c# - 远程运行 powershell 命令时出错

windows - 在 PowerShell 中写入具有多个作业的文件