Home > database >  Command Line Command Output in start-process from exe file
Command Line Command Output in start-process from exe file

Time:11-25

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:

./cctk.exe --wakeonlan

I can pipe the above command into a variable and get the information I need. This requires my shell to cd into the folder accordingly and run accordingly.

$test = ./cctk.exe --wakeonlan

This will give you an output. However when you use start-process, you get no output as this is a command-line command. A cmd screen appears and runs the command. So, I added a -nonewwindow and -wait flags. The output now appears on the screen, but I can't seem to capture it.

$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. 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.

$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. Some just open command prompt. The above says --wakeonlan is an unknown command.

Any pointers would help greatly.

CodePudding user response:

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

CodePudding user response:

To elaborate on js2010's helpful answer:

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.

If you do use Start-Process, which may be necessary in special situations, such as needing to run with a different user identity:

  • The only way to capture output is in text files, via the -RedirectStandardOutput / -RedirectStandardError parameters. Note that the character encoding of the output files is determined by the encoding stored in [Console]::OutputEncoding[1], which reflects the current console output code page, which defaults to the system's active legacy OEM code page.

  • 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.


[1] PowerShell uses the same encoding to decode output from external programs in direct invocations - see this answer for details.

  • Related