1

Is it possible to setup a script like this:

file.exe [CMD(POWERSHELL(CMD))]

Here's my code as of now, (combined from answers I've got here):

:RETURN
powershell -command "Start-Process -FilePath '\\my\file\path\myapplication.exe'"
powershell -command "if (gps | ? {$_.mainwindowtitle} | select name, id, mainwindowtitle | where {$_.Name -match 'myapplication'}) { Write-Output 'App verified, running in Taskbar.'} else { Write-Output 'App no running in Taskbar, checking again.' cmd.exe /c 'GOTO RETURN' }"

The above batch file has been converted into a .exe.

My main problem is cmd.exe /c 'GOTO RETURN' is being read as Write-Output instead of a code.

10
  • 5
    Don't do that. Please take a step back and describe the actual problem you're trying to solve instead of what you perceive as the solution. Commented Jul 10, 2019 at 11:09
  • @AnsgarWiechers I'm trying to loop the code to RETURN if the 'myapplication' is not displayed in the taskbar. Commented Jul 10, 2019 at 11:14
  • 1
    Even if you somehow got the cmd invocation correct, it still would't work. You cannot make a new, external cmd process execute a GOTO. GOTO only works inside the context of single batch file being interpreted. It is almost certainly possible to rewrite whatever it is you're doing entirely as a single PowerShell script, but if you're not willing to do that you'll at least have to remain within your original cmd session and check the exit code of the PowerShell command, so you can do the GOTO from within there as well. Commented Jul 10, 2019 at 11:15
  • @JeroenMostert would it be possible to call a .bat file instead inside the powershell -command "..." ? Commented Jul 10, 2019 at 11:18
  • @JeroenMostert one more question, is it possible write a powershell -command "..." with multiple lines? Commented Jul 10, 2019 at 11:22

1 Answer 1

1

So, if I parse correctly:

You have a cmd script, you are calling powershell to do some check and want to take an action based off the results of this powershell command.

If so, then you must change the call to powershell to be done inside a For /F loop to capture it's output.

This is a non-tested example I am doing on my phone:

 :RETURN

 #command lines here

 For /F "Tokens=*" %%A IN ('
   Powershell -command "if (gps ^| ? {$_.mainwindowtitle} ^| select name, id, mainwindowtitle ^| where {$_.Name -match ^'myapplication^'}^) { ^'App verified, running in Taskbar.^' } else { ^'App no running in Taskbar, checking again.^' }"
 ') DO (
   IF /I "%%A" EQU "App no running in Taskbar, checking again." (
     GOTO :RETURN
   )
 )
Sign up to request clarification or add additional context in comments.

1 Comment

It worked! I did some tweaks, I removed ^ . Thanks alot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.