使用 powershell 附加到 运行 程序

Attach to running program with powershell

我有一个 Raspberry Pi v2 运行ning Windows 10 物联网,我正在尝试 运行 它上面的 dotnet 2.0 控制台应用程序。

我有一个 powershell 脚本在启动时启动它,但是我没有从它获得控制台输出。我想知道是否有一种方法可以在我的计算机上使用 powershell 附加到 运行ning 进程,就像附加到 docker 容器一样,以获取控制台输出。

我不想 运行 我的程序在我的计算机上,并且 Raspberry Pi 是我唯一想在 运行 程序上过夜的计算机。

如果您不能或不想重新启动您的应用程序

在这种情况下,您的能力相当有限,因为正如 Harry Johnston 在另一篇 中所述:

Windows does not provide any mechanism to retroactively give another process a standard output handle if it was created without one. (Besides, in most cases an application will check the standard output handle only during startup.)

如果您需要读取已经写入控制台的内容,您可能就不走运了。 但是,您应该能够通过使用调试工具附加到进程来获得任何 future 输出。 在 Visual Studio Remote Debugger 的情况下,这甚至允许您调试控制台应用程序的执行,前提是您编写了它

accepted answer on Superuser.com suggests that StraceNT 还能够读取 运行 控制台应用程序的输出。如果 运行 应用程序不是您自己编写的,这可能是您最好的选择。

如果可以停止并重新启动应用程序

Powershell Remoting 可能是最好的方法。

您需要在 Raspberry Pi 上 enable Powershell Remoting,然后您可以在本地 Powershell 控制台中使用 enter-pssession [RaspberryPi-Hostname-or-IP] 连接到您的 Raspberry Pi。

此时您可以像在本地机器上一样启动您的控制台应用程序 运行 并获得您想要的所有控制台输出。

无需附加任何东西。

您可以将控制台应用程序的输出重定向到当前目录中的 txt 文件。如果您 运行 来自 powershell 的控制台应用程序,您可以这样做:

.\[YOURAPP].exe | tee test.txt

并且您可以从 Windows file sharing 访问 test.txt。