首页 > 解决方案 > 如何输出一个持续激活的 CMD

问题描述

我正在运行一个打开 CMD 并通过 API 服务连接的应用程序。一整天,新的东西都会出现在 CMD 中,我想将该信息导出到某个地方的 txt 文件中,并且每次出现新的东西时都会附加到同一个文件中,或者创建一个新文件。没关系

App.exe > /file.txt 并没有真正起作用

标签: windowspowershellcmdappendoutput

解决方案


重定向示例

  command >  filename     # Redirect command output to a file (overwrite)
  command >> filename     # APPEND into a file
  command 2> filename     # Redirect Errors from operation to a file(overwrite)
  command 2>> filename    # APPEND errors to a file
  command 2>&1            # Add errors to results
  command 1>&2            # Add results to errors
  command | command       # This is the basic form of a PowerShell Pipeline

# In PowerShell 3.0+

  command 3> warning.txt  # Write warning output to warning.txt 
  command 4>> verbose.txt # Append verbose.txt with the verbose output 
  command 5>&1            # Writes debug output to the output stream 
  command *> out.txt      # Redirect all streams (output, error, warning, verbose, and debug) to out.txt

您没有显示任何关于如何为您的用例启动/使用 cmd.exe 的代码。这只是让人们试图帮助你,猜测。所以,重定向 cmd.exe,例如:

$MyOutputFile = C:\MyOutputFile.txt
Start-Process -FilePath c:\windows\system32\cmd.exe -ArgumentList '/c C:\YourCommand.bat' -Wait -NoNewWindow -RedirectStandardOutput $MyOutputFile

最后,因为你让我们猜测。如果您从 PowerShell 启动进程 A,但进程 A 反过来又启动进程 B,则由进程 A 来捕获或重定向进程 B 的输出。PowerShell 无法分如果进程 A 没有这样做,则捕获。

资源


推荐阅读