`more.com` returns "Not enough memory."

`more.com` returns "Not enough memory."

环境详细信息:


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

Not enough memory.

当我尝试执行 helpman 时。无论我使用本机 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
}

更进一步... 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
    }
}

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

Not enough memory.

错误。

我没有足够的知识来弄清楚为什么它会抛出该消息或如何在不同的系统上复制它(例如,我无法在 x64 上复制Windows 10 1804 Pro),但可以通过将 [System.Console] 上的 OutputEncoding 静态成员更改为默认编码(在本例中,CP437,这是我的 conhost 的默认编码)来修复它:

[System.Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(437)

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

如果在 中观察到此错误,可以使用 chcp.com 修复(未测试这是否也会更新 [Console]::OutputEncoding):

chcp.com 437

作为旁注,导致我失败的代码在我的 $PROFILE:

[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.UTF8Encoding]::new()

即使在退出 后,当我留在 提示符下时,问题仍然存在。


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

[Console]::OutputEncoding = [Text.Encoding]::GetEncoding(437)
& chcp.com 437