`more.com` returns “Not enough memory.”

天大地大妈咪最大 提交于 2021-02-07 14:32:50

问题


Environment details:

  • x64 Win7 SP1 Enterprise
  • Windows PowerShell v5.0

Without any profiles loaded, my local powershell sessions are returning

Not enough memory.

when I try to execute help or man. This occurs whether I'm using the native powershell.exe or conemu.

Strangely, I am able to execute any other aliases I've tried, and it doesn't add to the $Error variable, so I have no idea where to start troubleshooting (I've tried -ErrorAction Stop and $ErrorActionPreference = 'Stop').

As a footnote, I don't have any elevated privileges.


After some exploration, I found that man is actually an alias for help which isn't an alias for Get-Help, but a function of its own with this definition:

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
}

Even further... more is another function:

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

    $OutputEncoding = [System.Console]::OutputEncoding

    if($paths) {
        foreach ($file in $paths) {
            Get-Content $file | more.com
        }
    }
    else {
        $input | more.com
    }
}

回答1:


A seemingly inherent flaw with more.com, it has difficulty handling multi-byte encodings (such as utf-8) and will instead throw a

Not enough memory.

error.

I don't have enough knowledge to figure out why it throws that message or how to replicate it on different systems (for example, I could not replicate on x64 Windows 10 1804 Pro), but it can be remediated by changing the OutputEncoding static member on [System.Console] to a default encoding (in this case, CP437, which was my conhost's default):

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

or other single-byte encoding, such as CP1252.

If this error is being observed in cmd, it can be remediated using chcp.com (untested whether this also updates [Console]::OutputEncoding):

chcp.com 437

As a side-note, the code that caused this failure for me is in my $PROFILE:

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

The issues persisted while using the same console host, even after exiting powershell when I was left in a cmd prompt.


Edit: for powershell to work, I had to do a combined codepage + OutputEncoding change:

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


来源:https://stackoverflow.com/questions/55248656/more-com-returns-not-enough-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!