[英]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.上述批处理文件已转换为 .exe。
My main problem is cmd.exe /c 'GOTO RETURN'
is being read as Write-Output
instead of a code.我的主要问题是
cmd.exe /c 'GOTO RETURN'
被读取为Write-Output
而不是代码。
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.您有一个 cmd 脚本,您正在调用 powershell 进行一些检查,并希望根据此 powershell 命令的结果采取行动。
If so, then you must change the call to powershell to be done inside a For /F
loop to capture it's output.如果是这样,那么您必须更改要在
For /F
循环内完成的对 powershell 的调用以捕获它的输出。
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
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.