`more.com`返回“内存不足”。

The*_*le1 5 windows powershell cmd powershell-5.0

环境详细信息:

  • x64 Win7 SP1企业
  • Windows PowerShell v5.0

没有加载任何配置文件,我的本地会话将返回

内存不足。

当我尝试执行helpman。无论我使用的是native powershell.exe还是发生这种情况。

奇怪的是,我能够执行我尝试过的任何其他别名,并且它不会添加到$Error变量中,所以我不知道从哪里开始进行故障排除(我已经尝试过-ErrorAction Stop$ErrorActionPreference = 'Stop')。

作为注脚,我没有任何特权。


经过一番探索,我发现它man实际上是一个别名,help不是的别名Get-Help,而是具有以下定义的函数:

function help {
<#
.FORWARDHELPTARGETNAME Get-Help
.FORWARDHELPCATEGORY Cmdlet
#>
    [CmdletBinding(DefaultParameterSetName = 'AllUsersView', HelpUri = 'http://go.microsoft.com/fwlink/?LinkID=113316')]
    param(
        [Parameter(Position = 0, ValueFromPipelineByPropertyName = $true)]
        [string]
        ${Name},

        [string]
        ${Path},

        [ValidateSet('Alias', 'Cmdlet', 'Provider', 'General', 'FAQ', 'Glossary', 'HelpFile', 'ScriptCommand', 'Function', 'Filter', 'ExternalScript', 'All', 'DefaultHelp', 'Workflow', 'DscResource', 'Class', 'Configuration')]
        [string[]]
        ${Category},

        [string[]]
        ${Component},

        [string[]]
        ${Functionality},

        [string[]]
        ${Role},

        [Parameter(ParameterSetName = 'DetailedView', Mandatory = $true)]
        [switch]
        ${Detailed},

        [Parameter(ParameterSetName = 'AllUsersView')]
        [switch]
        ${Full},

        [Parameter(ParameterSetName = 'Examples', Mandatory = $true)]
        [switch]
        ${Examples},

        [Parameter(ParameterSetName = 'Parameters', Mandatory = $true)]
        [string]
        ${Parameter},

        [Parameter(ParameterSetName = 'Online', Mandatory = $true)]
        [switch]
        ${Online},

        [Parameter(ParameterSetName = 'ShowWindow', Mandatory = $true)]
        [switch]
        ${ShowWindow}
    )

    #Set the outputencoding to Console::OutputEncoding. More.com doesn't work well with Unicode.
    $outputEncoding = [System.Console]::OutputEncoding

    Get-Help @PSBoundParameters | more
}
Run Code Online (Sandbox Code Playgroud)

更进一步... more是另一个功能:

function more {
    param([string[]]$paths)

    $OutputEncoding = [System.Console]::OutputEncoding

    if($paths) {
        foreach ($file in $paths) {
            Get-Content $file | more.com
        }
    }
    else {
        $input | more.com
    }
}
Run Code Online (Sandbox Code Playgroud)

The*_*le1 5

的一个看似内在的缺陷more.com,它难以处理多字节编码(例如),而会抛出一个

内存不足。

错误。

我没有足够的知识来弄清楚为什么它会引发该消息或如何在不同系统上复制该消息(例如,我无法在x64 Windows 10 1804 Pro上复制),但是可以通过更改以下位置的OutputEncoding静态成员来对其进行补救[System.Console]转换为默认编码(在本例中为CP437,这是我的主持人的默认编码):

[System.Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(437)
Run Code Online (Sandbox Code Playgroud)

或其他单字节编码,例如CP1252

如果在中观察到此错误,则可以使用来纠正该错误chcp.com(无论是否也更新[Console]::OutputEncoding):

chcp.com 437
Run Code Online (Sandbox Code Playgroud)

附带说明一下,导致我失败的代码在my中$PROFILE

[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.UTF8Encoding]::new()
Run Code Online (Sandbox Code Playgroud)

使用同一控制台主机时,即使在我离开提示符后退出之后,问题仍然存在。


编辑:为了使powershell正常工作,我必须进行组合代码页+ OutputEncoding更改:

[Console]::OutputEncoding = [Text.Encoding]::GetEncoding(437)
& chcp.com 437
Run Code Online (Sandbox Code Playgroud)