简体   繁体   English

从 exe 文件的启动过程中的命令行命令 Output

[英]Command Line Command Output in start-process from exe file

Here is the program.这是程序。 I am using dell command |我正在使用戴尔命令 | configure.配置。 The command-line command is as follows:命令行命令如下:

"C:\Program Files (x86)\Dell\Command Configure\X86_64>cctk.exe" --wakeonlan

In Powershell you can navigate to the folder and run:在 Powershell 中,您可以导航到文件夹并运行:

./cctk.exe --wakeonlan

I can pipe the above command into a variable and get the information I need.我可以把上面的命令 pipe 变成一个变量,得到我需要的信息。 This requires my shell to cd into the folder accordingly and run accordingly.这需要我的 shell 相应地进入文件夹并相应地运行。

$test = ./cctk.exe --wakeonlan

This will give you an output.这将为您提供 output。 However when you use start-process, you get no output as this is a command-line command.但是,当您使用 start-process 时,您不会得到 output 因为这是一个命令行命令。 A cmd screen appears and runs the command.出现 cmd 屏幕并运行命令。 So, I added a -nonewwindow and -wait flags.因此,我添加了 -nonewwindow 和 -wait 标志。 The output now appears on the screen, but I can't seem to capture it. output 现在出现在屏幕上,但我似乎无法捕捉到它。

$test = start-process "C:\Program Files (x86)\Dell\Command Configure\X86_64\cctk.exe" -ArgumentList @("--wakeonlan") -NoNewWindow -Wait

At this point test is empty.此时测试为空。 I tried using the Out-File to capture the information as well.我也尝试使用 Out-File 来捕获信息。 No success.没有成功。 The command outputs to the screen but nowhere else.该命令输出到屏幕但没有其他地方。

I also tried the cmd method where you pipe the information in using the /C flag.我还尝试了 cmd 方法,您可以在其中 pipe 使用 /C 标志的信息。

$test = Start-Process cmd -ArgumentList '/C start "C:\Program Files (x86)\Dell\Command Configure\X86_64\cctk.exe" "--wakeonlan"' -NoNewWindow -Wait

However, I have tried many variations of this command with no luck.但是,我已经尝试了这个命令的许多变体,但没有运气。 Some say C:\Program is not recognized.有人说 C:\Program 无法识别。 Some just open command prompt.有些只是打开命令提示符。 The above says --wakeonlan is an unknown command.上面说 --wakeonlan 是一个未知命令。

Any pointers would help greatly.任何指针都会有很大帮助。

There are various ways to run this without the added complication of start-process.有多种方法可以运行它,而不会增加启动过程的复杂性。

Add to the path temporarily:临时添加到路径中:

$env:path += ';C:\Program Files (x86)\Dell\Command Configure\X86_64;'
cctk

Call operator:呼叫接线员:

& 'C:\Program Files (x86)\Dell\Command Configure\X86_64\cctk'

Backquote all spaces and parentheses:反引号所有空格和括号:

C:\Program` Files` `(x86`)\Dell\Command` Configure\X86_64\cctk

To elaborate on js2010's helpful answer :详细说明js2010 的有用答案

In short: Because your executable path is quoted , direct invocation requires use of & , the call operator , for syntactic reasons - see this answer for details.简而言之:由于您的可执行路径被引用,因此出于语法原因直接调用需要使用& 调用运算符- 请参阅此答案以获取详细信息。

To synchronously execute console applications or batch files and capture their output, call them directly ( $output = c:\path\to\some.exe... or $output = & $exePath... ), do not use Start-Process (or the System.Diagnostics.Process API it is based on) - see this answer for more information.同步执行控制台应用程序或批处理文件并捕获它们的 output,请直接调用它们$output = c:\path\to\some.exe...$output = & $exePath... ),不要使用Start-Process (或它所基于的System.Diagnostics.Process API) - 请参阅此答案以获取更多信息。

If you do use Start-Process , which may be necessary in special situations, such as needing to run with a different user identity :如果您确实使用Start-Process ,这在特殊情况下可能是必要的,例如需要以不同的用户身份运行:

  • The only way to capture output is in text files , via the -RedirectStandardOutput / -RedirectStandardError parameters.捕获 output 的唯一方法是在文本文件中,通过-RedirectStandardOutput / -RedirectStandardError参数。 Note that the character encoding of the output files is determined by the encoding stored in [Console]::OutputEncoding , which reflects the current console output code page, which defaults to the system's active legacy OEM code page.请注意,output 文件的字符编码由存储在[Console]::OutputEncoding中的编码确定,它反映了当前控制台 output 代码页,默认为系统的活动旧版OEM代码页。

  • By contrast, even with -NoNewWindow -Wait , directly capturing output with $output =... does not work, because the launched process writes directly to the console , bypassing PowerShell's success output stream, which is the one whose content variable assignments capture. By contrast, even with -NoNewWindow -Wait , directly capturing output with $output =... does not work, because the launched process writes directly to the console , bypassing PowerShell's success output stream, which is the one whose content variable assignments capture.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM