简体   繁体   English

通过Powershell远程会话执行时,程序输出是不同的

[英]Program output is different when executed through Powershell remote session

I'm developing a Windows Console Application, using Visual Studio 2010, which prints progress percentage on screen such that the last number gets overwritten with the current number. 我正在使用Visual Studio 2010开发Windows控制台应用程序,该应用程序会在屏幕上打印进度百分比,以便最后一个数字被当前数字覆盖。 I'm using carriage return to achieve this like so: 我正在使用回车来实现这一点,就像这样:

std::wcout
    << "[Running: "
    << std::setw(3)
    << std::setprecision(3)
    << percent
    << "%]"
    << '\r'
;

When I execute this program through PowerShell on a remote host using PSSesion: 当我使用PSSesion在远程主机上通过PowerShell执行此程序时:

Enter-PSSession WWW.XXX.YYY.ZZZ -credential <UserName>
[WWW.XXX.YYY.ZZZ]: PS C:\> .\test.exe

it behaves differently compared to when its executed locally either in PowerShell or in cmd.exe. 与在PowerShell或cmd.exe中本地执行时相比,它的行为有所不同。 I face following three problems: 我面临以下三个问题:

1) The console output of the program is different, in that, instead of overwriting [Running xx%] its printing in the next line like so: 1)该程序的控制台输出是不同的,它不是在下一行中覆盖[Running xx%]的打印,如下所示:

[Running  1%]
[Running  2%]
[Running  3%]
[Running  4%]
...

This is similar to what happens when output of a program is redirected to a file (lone carriage returns or newlines are replaced with carriage returns + newlines combinations). 这类似于将程序的输出重定向到文件时发生的情况(将单独的carriage returnsnewlines替换为carriage returns + newlines组合)。

2) The output doesn't shows up as and when the program writes something to cout. 2)当程序向cout写入内容时,输出不会显示。 It comes at once at the end of execution. 它在执行结束时立即出现。 Does powershell caches the output of remote program and sends to the caller only once? Powershell是否缓存远程程序的输出并仅发送给调用者一次? If there is significant time difference between two lines (line meaning all the output between two newline s) then the first line gets printed as if it waits for some time for another newline and if received, it again goes to waiting state, if not received within certain time period (~500ms), it sends the output till last newline (and not all the accumulated output) to the caller. 如果两行之间存在明显的时间差(该行表示两个newline之间的所有输出),则打印第一行,就像它等待一段时间等待另一个newline ,如果收到,则再次进入等待状态(如果未收到)在特定时间段(约500ms)内,它将输出发送到最后一个newline (而不是所有累积的输出)给调用方。 As can be seen from my code, there is no newline resulting in all the [Running xx%] being printed at once at the end. 从我的代码可以看出,没有newline导致所有[Running xx%]都一次打印在末尾。

3) At certain point in the program I need user's confirmation, but cin.fail() returns true in remote execution. 3)在程序中的某些点上,我需要用户确认,但是cin.fail()在远程执行中返回true So, is there any way to take user input in such an execution environment? 那么,在这种执行环境中,是否有任何方法可以接受用户输入? Or is there any way to detect that the program is being executed remotely through Powershell (eg. some env variable)? 还是有什么方法可以检测到该程序正在通过Powershell远程执行(例如,某些env变量)?

Any help related to any point will be greatly appreciated. 与任何问题有关的任何帮助将不胜感激。 :) :)

With respect to 1), the WinRM protocol for PowerShell Remoting is really just an input/output stream for commands and their serialized output. 关于1),用于PowerShell Remoting的WinRM协议实际上只是命令及其序列化输出的输入/输出流。 It has no terminal emulation capabilities, so when you run things remotely, their output is fed back sequentially one line at a time. 它没有终端仿真功能,因此,当您远程运行事物时,它们的输出一次会依次反馈到一行。 If you used SSH or Telnet to a remote shell of powershell.exe, you would likely see what you expect. 如果您使用SSH或Telnet到powershell.exe的远程外壳,则可能会看到期望的结果。

As for 2), the default output buffermode appears to be "Block" so this would explain the behaviour you're seeing. 对于2),默认的输出缓冲模式似乎是“阻止”,因此可以解释您所看到的行为。 You can change this with the New-PSSessionOption cmdlet to create a configuration for a session created by New-PSSession that can be passed to the -Session parameter on remoting aware cmdlets. 您可以使用New-PSSessionOption cmdlet更改此设置,以为New-PSSession创建的会话创建配置,该配置可以在远程感知cmdlet时传递给-Session参数。 The option is OutputBufferingMode . 选项是OutputBufferingMode If you set it to None , you should get the desired behaviour. 如果将其设置为None ,则应该获得所需的行为。

# default output buffering mode is "none" for default options
$s = new-pssession localhost -sessionoption (new-pssessionoption)
enter-pssession $s

For 3), you should be able to accept input interactively IIRC. 对于3), 您应该能够以交互方式接受IIRC输入。 Looks like interactive stuff doesn't work right - I may have completely misremembered this. 似乎交互式内容无法正常工作-我可能完全忘记了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 远程执行程序时崩溃,但不是本地执行 - Program crashes when executed remotly, but not locally 通过php远程实现c ++程序 - remote implementing c++ program through php C++ 程序在执行时什么都不做 - C++ program does nothing when executed C ++:执行时程序“无响应” - C++: Program “Not Responding” When Executed 为什么在使用mingw进行编译时,与g ++相比,我的程序给出的输出完全不同? - Why is my program giving a totally different output when I compile with mingw as compared to g++ 在执行任何程序代码之前,通过静态 .lib 链接到 dll 的程序会发生什么? - What happens in a program that is linked to a dll through a static .lib before any of the program code is executed? Java和C ++中的类似程序的不同输出 - Similar program in java and c++ different output 为什么下面的 Hello World 程序在 PowerShell 上没有显示任何 output? 相同的程序在 CMD 上显示正确的 output - Why is the following Hello World program not showing any output on PowerShell ? The same program show correct output on CMD 当有两个或多个递归函数写入geather时,程序如何执行? - how is program executed when there are 2 or more recursion function written togeather? 在shell中执行但不在启动/自动化脚本中执行时,程序(nload)作为守护程序运行 - Program (nload) runs as a daemon when executed in shell but not in startup/automation script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM