0

I am trying to run cmd command using Powershell.

CMD

cd C:\apache-jmeter-5.2.1\bin
jmeter -n -t C:\User-search.jmx -l C:\Result.jtl

Powershell

$command = @'
cmd.exe /C C:\apache-jmeter-5.2.1\bin\jmeter -n -t C:\User-search.jmx -l C:\Result.jtl
'@

Invoke-Expression -Command:$command

Error

cmd.exe : The term 'cmd.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
rAJ
  • 1,295
  • 5
  • 31
  • 66
  • In short: [`Invoke-Expression` should generally be avoided](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/); definitely [don't use it to invoke an external program](https://stackoverflow.com/a/57966347/45375). Additionally, there's generally no need to call an external program via `cmd.exe` (`cmd /c ...`), just invoke it directly. – mklement0 Feb 24 '20 at 18:24
  • It's a moot point, because invoking `C:\apache-jmeter-5.2.1\bin\jmeter -n -t C:\User-search.jmx -l C:\Result.jtl` as-is solves your problem, but your symtpom can't be explained with the code you've posted. – mklement0 Feb 24 '20 at 18:28
  • The two commands in CMD -> runs perfectly using command line. I am just trying to merge the two-line into one and want to run through Powershell. – rAJ Feb 24 '20 at 18:31
  • Then use the command from my previous comment _as is_ - no need for the ill-advised `Invoke-Expression` or any other cmdlets. PowerShell is a _shell_ - that means it can invoke external programs _directly_ - see the linked post for more information. – mklement0 Feb 24 '20 at 18:34
  • Thanks. but in that case, i am facing error -> Not able to find Java executable or version. Please check your Java installation. errorlevel=2 – rAJ Feb 24 '20 at 18:36
  • That is an _unrelated_ problem; it is evidence that the call worked in principle, but that the program you're calling cannot locate `java.exe`; the one difference between your `cmd` command and what you tried in PowerShell is that you didn't `cd` to `C:\apache-jmeter-5.2.1\bin` first in PowerShell, so perhaps that is the reason - simply run `cd C:\apache-jmeter-5.2.1\bin` in PowerShell first as well (`cd` in PowerShell is a built-in alias for the `Set-Location` cmdlet). – mklement0 Feb 24 '20 at 18:40
  • no luck. same error. should I post a new question for this? – rAJ Feb 24 '20 at 19:11
  • Please do, and please use the direct-invocation method I suggested, along with what `gcm java.exe` shows in PowerShell and how `%PATH%` in `cmd.exe` differs from `$env:Path` in PowerShell. – mklement0 Feb 24 '20 at 19:15

0 Answers0