简体   繁体   中英

Nested cmd.exe command within a powershell.exe command called through cmd.exe

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.

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
   )
 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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