首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell Read-Host仅接受最多约8000个字符

Powershell Read-Host仅接受最多约8000个字符
EN

Stack Overflow用户
提问于 2020-02-04 02:44:42
回答 1查看 224关注 0票数 0

我需要使用read-host来获得一个非常长的字符串输入(大约9000个字符),但是read-host会截断大约8000个字符,有人知道如何扩展read-host的最大字符数吗?

EN

回答 1

Stack Overflow用户

发布于 2021-12-05 05:20:49

以下是可能的解决方法。变通方法1的优点在于,它可以处理需要键盘输入的PowerShell后台作业。请注意,如果您尝试粘贴包含新行的剪贴板内容,Read-HostLine将只读取第一行,但Read-Host具有相同的行为。

解决方法1:

代码语言:javascript
复制
<#
.SYNOPSIS
Read a line of input from the host.

.DESCRIPTION
Read a line of input from the host. 

.EXAMPLE
$s = Read-HostLine -prompt "Enter something"

.NOTES
Read-Host has a limitation of 1022 characters.
This approach is safe to use with background jobs that require input.
If pasting content with embedded newlines, only the first line will be read.
A downside to the ReadKey approach is that it is not possible to easily edit the input string before pressing Enter as with Read-Host.
#>
function Read-HostLine ($prompt = $null) {
    if ($prompt) {
        "${prompt}: " | Write-Host
    }

    $str = ""
    while ($true) { 
        $key = $host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown"); 

        # Paste the clipboard on CTRL-V        
        if (($key.VirtualKeyCode -eq 0x56) -and  # 0x56 is V
            (([int]$key.ControlKeyState -band [System.Management.Automation.Host.ControlKeyStates]::LeftCtrlPressed) -or 
                ([int]$key.ControlKeyState -band [System.Management.Automation.Host.ControlKeyStates]::RightCtrlPressed))) { 
            $clipboard = Get-Clipboard
            $str += $clipboard
            Write-Host $clipboard -NoNewline
            continue
        }
        elseif ($key.VirtualKeyCode -eq 0x08) {  # 0x08 is Backspace
            if ($str.Length -gt 0) {
                $str = $str.Substring(0, $str.Length - 1)
                Write-Host "`b `b" -NoNewline    
            }
        }        
        elseif ($key.VirtualKeyCode -eq 13) {  # 13 is Enter
            Write-Host
            break 
        }
        elseif ($key.Character -ne 0) {
            $str += $key.Character
            Write-Host $key.Character -NoNewline
        }
    }

    return $str
}

解决方法2:

代码语言:javascript
复制
$maxLength = 65536
[System.Console]::SetIn([System.IO.StreamReader]::new([System.Console]::OpenStandardInput($maxLength), [System.Console]::InputEncoding, $false, $maxLength))

$s = [System.Console]::ReadLine()

解决方法3:

代码语言:javascript
复制
function Read-Line($maxLength = 65536) {
    $str = ""
    $inputStream = [System.Console]::OpenStandardInput($maxLength);
    $bytes = [byte[]]::new($maxLength);
    while ($true) {
        $len = $inputStream.Read($bytes, 0, $maxLength);
        $str += [string]::new($bytes, 0, $len)
        if ($str.EndsWith("`r`n")) {
            $str = $str.Substring(0, $str.Length - 2)
            return $str
        }
    }
}

$s = Read-Line

这里有更多的讨论:

Console.ReadLine() max length?

Why does Console.Readline() have a limit on the length of text it allows?

https://github.com/PowerShell/PowerShell/issues/16555

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60045306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档