【发布时间】:2021-10-12 19:56:55
【问题描述】:
我见过Batch macro not working - 但它没有解决我的问题。
基本上,我看到了:
- Set output of a command as a variable (with pipes)
- Assign output of a program to a variable using a MS batch file
...我想,让我试试吧。
首先,我将其保存在test.bat 文件中:
powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id"
pause
我双击这个test.bat,我得到:
C:\tmp>powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id"
10968
C:\tmp>pause
Press any key to continue . . .
所以,好的,它有效。
然后我决定将上面的代码包装在一个“批处理宏”中——所以test.bat 变成了:
%$set% TESTPID="powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id""
echo TESTPID %TESTPID%
pause
现在当我双击这个test.bat 时,它不起作用,我得到:
C:\tmp>TESTPID="powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id""
'TESTPID' is not recognized as an internal or external command,
operable program or batch file.
C:\tmp>echo TESTPID
TESTPID
C:\tmp>pause
Press any key to continue . . .
好像%$set% 被解释为一个变量,因此被视为空,然后执行该行的其余部分......
那么,我哪里出错了 - 在这种情况下,我可以让这个“批处理宏”工作吗?如果可以,怎么做?
【问题讨论】:
标签: windows batch-file