I've seen Batch macro not working - but it does not address my question.
Basically, I saw:
- Set output of a command as a variable (with pipes)
- Assign output of a program to a variable using a MS batch file
... and I thought, let me try it.
So first, I save this in a test.bat
file:
powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id"
pause
I double-click this test.bat
, and I get:
C:\tmp>powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id"
10968
C:\tmp>pause
Press any key to continue . . .
So, ok, it works.
Then I decide to wrap the above code in a "batch macro" - so test.bat
becomes instead:
%$set% TESTPID="powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id""
echo TESTPID %TESTPID%
pause
Now when I double-click this test.bat
, it does not work, and I get:
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 . . .
It's as if the %$set%
was interpreted as a variable, and as such was taken as empty, and then the rest of the line was executed ...
So, where am I going wrong - and can I get this "batch macro" to work in this case, and if so, how?