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.

Compo
  • 36,585
  • 5
  • 27
  • 39
pjustindaryll
  • 377
  • 3
  • 14
  • 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. – Ansgar Wiechers Jul 10 '19 at 11:09
  • @AnsgarWiechers I'm trying to loop the code to RETURN if the 'myapplication' is not displayed in the taskbar. – pjustindaryll Jul 10 '19 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. – Jeroen Mostert Jul 10 '19 at 11:15
  • @JeroenMostert would it be possible to call a ````.bat```` file instead inside the ````powershell -command "..."```` ? – pjustindaryll Jul 10 '19 at 11:18
  • @JeroenMostert one more question, is it possible write a ````powershell -command "..."```` with multiple lines? – pjustindaryll Jul 10 '19 at 11:22
  • Possible yes, but why are you using batch at all instead of just writing powershell? – Maximilian Burszley Jul 10 '19 at 11:28
  • If, for whatever reason, you had a need to embed a complex script in a batch file, and you couldn't use an external file, you can use the `-EncodedCommand` parameter to feed PowerShell a Base64-encoded version of the script. This is much more convenient than futzing around with quoting. However, I can't see any reason to use `cmd` here in the first place; the whole thing could be written in PowerShell. There do exist converters to make an executable out of a PowerShell script as well. At the very least, the loop could be done in PowerShell. – Jeroen Mostert Jul 10 '19 at 11:29
  • If your problem-case is that you need users to be able to double-click your script, you can use a batch file wrapper for powershell and I've recently written [an expanded answer here](https://stackoverflow.com/questions/50778734/unable-to-execute-powershell-script-using-run-with-powershell-option/50789772#50789772) – Maximilian Burszley Jul 10 '19 at 11:30
  • @JeroenMostert, apologies, I'm relatively new when it comes to PowerShell, I'm using **Bat to Exe Converter** to convert my script to **.exe**. I think I'll go with your suggestion to write everything to PowerShell instead. – pjustindaryll Jul 10 '19 at 11:41
  • @JeroenMostert or if there's a way to do a multiple line ````powershell -command ".."```` it would be a great help. I can only write a single line using it. – pjustindaryll Jul 10 '19 at 11:42

1 Answers1

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
   )
 )
Ben Personick
  • 3,074
  • 1
  • 22
  • 29