我正在运行一个打开CMD并通过API服务进行连接的应用程序。整天,新的内容都会显示在CMD中,我想将该信息导出到txt中,每次出现新的内容时,都会将该内容附加到同一文件中,或创建一个新文件。没关系
App.exe> /file.txt无效
重定向示例
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没有执行,则捕获。
资源