PowerShell高级配置


场景:

远程执行PowerShell脚本传递PSObject对象,提示从远程客户端计算机接收的数据的当前反序列化对象大小超过允许的最大对象大小。当前反序列化对象大小为10572800,允许的最大对象大小为10485760。如下图:

PowerShell高级配置_PowerShell

修改远程服务器默认配置限制:

Set-PSSessionConfiguration -name Microsoft.powershell -MaximumReceivedDataSizePerCommandMB 20 -MaximumReceivedObjectSizeMB 20

PowerShell高级配置_PowerShell_02

提示WSMan提供程序主机进程未能返回适当的响应。主机进程中的提供程序的行为不正确。如下图:

PowerShell高级配置_OutOfMemory_03

查看本地PowerShell日志:

PowerShell高级配置_OutOfMemory_04

PowerShell高级配置_OutOfMemory_05

查看远程服务器上PowerShell日志:

PowerShell高级配置_OutOfMemory_06

有System.OutOfMemoryException错误。重新执行脚本,使用资源监视器观察内存占用,以工作集倒序排序,观察到wsmprovhost.exe进程最大为512MB,脚本运行失败。修改Shell内存配置,暂时不修改插件的内存配置:

PowerShell高级配置_PowerShell_07

再次观察内存使用,wsmprovhost.exe已超过512MB限制,脚本返回成功。

PowerShell高级配置_OutOfMemory_08

参考:

Learn How to Configure PowerShell Memory

https://stackoverflow.com/questions/13561730/maximum-data-size-in-a-remote-command