gpt4 book ai didi

java - 如果控制台输出长度超过 4119 个字符,则从 Java 远程运行 PowerShell 命令会挂起

转载 作者:行者123 更新时间:2023-11-30 07:46:08 24 4
gpt4 key购买 nike

从 Java 运行以下命令:

powershell.exe -Command "& {$SecurePassword = ConvertTo-SecureString -AsPlainText $env:remotePass -Force; $Credentials = New-Object System.Management.Automation.PSCredential $env:remoteUser, $SecurePassword; $remoteSession = New-PSSession -ComputerName $env:targetComputerName -Credential $Credentials; Invoke-Command -Session $remoteSession -ScriptBlock {C:\temp\dummy.ps1};}"

如果远程服务器上的 dummy.ps1 输出超过 4119 个字符,则会在 Java 端挂起。更好地说,它在远程计算机上很好地终止,但不知何故,对于较长的输出,终止字符不会到达调用者。当然,如果有一些终止字符,但无论如何这是我最好的猜测。

从控制台执行相同的操作不会出现问题。

字符数限制接近 4K,但超出了它。所以一开始听起来很有希望。

有人有过这种现象的经历,或者对瓶颈有什么想法吗?

提前谢谢您!

最佳答案

您可能会遇到缓冲区已满的问题。当您执行runtime.exec时,您需要消耗控制台应用程序将填充的缓冲区。当缓冲区满时,进程将挂起。

请参阅这篇优秀文章中的“为什么 Runtime.exec() 挂起”部分: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

或者看这里 Capturing stdout when calling Runtime.exec

关于java - 如果控制台输出长度超过 4119 个字符,则从 Java 远程运行 PowerShell 命令会挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com