0

I've got a VERY big pre-existing batch file that I need to add some logic to run the correct EXE depending on whether it is a x86 or x64 bit machine. I can't use PowerShell because for some reason they have it pseudo disabled on most of these machines.

What I'm trying to do is use PSEXEC to fetch the system variable PROCESSOR_ARCHITECTURE from the remote, then create a local system variable from the output.

If I run this command from a Win10x64 CMD prompt:

C:\PsTools>c:\pstools\PSEXEC.exe \\TEST-Win7x86 cmd /c echo ^%PROCESSOR_ARCHITECTURE^%

I get the correct remote x86 value, but can't do anything with it:

x86
cmd exited on TEST-Win7x86 with error code 0

.

I tried testing this method to get the output into a variable:

FOR /F "delims=" %i IN ('c:\pstools\PSEXEC.exe \\TEST-Win7x86 cmd /c echo ^%PROCESSOR_ARCHITECTURE^%') DO set proc=%i

I'm getting back the local AMD64 value:

cmd exited on TEST-Win7x86 with error code 0.
C:\PsTools>set proc=AMD64

.

Anyone spot what I'm doing wrong?

Thanks!

-Matt

Community
  • 1
  • 1
EvilBetty
  • 1
  • 2
  • 1
    Are you aware that you have to double the `%` signs of `for` variables when being used within batch files? Also I think you need to write `^^%%PROCESSOR_ARCHITECTURE^^%%` in the `for /F` loop in order to pass the string `^%PROCESSOR_ARCHITECTURE^%` over literally... – aschipfl May 17 '16 at 22:45
  • You could also avoid the percent sign ambiguity by doing `psexec \\remotepc powershell "$env:processor_architecture"`. Or you could skip `psexec` and use a remote WMI query. `for /f %I in ('wmic /node:remotepc os get osarchitecture /value ^| find "="') do set "%I"` – rojo May 18 '16 at 01:51
  • I use the PowerShell method most of the time, but in this case these computers have something in place preventing PowerShell from working. I didn't think of using WMIC to fetch it. I'll remember that for next time, thanks! – EvilBetty May 18 '16 at 21:09

1 Answers1

0

After messing with this most of the day, I think I have it figured out shortly after submitting this question. I had to double-up the carrots for some reason. I'm not exactly sure why I thought to try it or why it works, but I'm getting the correct output now.

C:\PsTools>FOR /F %i IN ('c:\pstools\PSEXEC.exe \\TEST-Win7x86 cmd /c echo ^^%PROCESSOR_ARCHITECTURE^^%') DO set proc=%i

Output:

cmd exited on TEST-KW-O3040MD with error code 0.

C:\PsTools>set proc=x86

.

And as aschipfl corrected below, this is the correct context for in the batch script. Thanks aschipfl!

FOR /F %%i IN ('"\\Server\Shared Folder\PSEXEC.exe" \\TEST-Win7x86 cmd /c echo ^^%%PROCESSOR_ARCHITECTURE^^%%') DO set PROC=%%i
EvilBetty
  • 1
  • 2
  • This can only work in case you execute it in command prompt (`cmd`) directly; in a batch file, you'll need to double the `%` signs of the `for` variable `%i` as I already suggested, and the ones in the `IN ( ... )` part too... – aschipfl May 17 '16 at 23:00
  • Well that worked for the command line, but now it's broke in the script. Running this command: . . `FOR /F %%i IN ('"\\Server\Shared Folder\PSEXEC.exe" \\%CNAME% cmd /c echo ^%PROCESSOR_ARCHITECTURE^%') DO set PROC=%%i` Results in the local AMD64 again. Doubling up carrots seems to kill it. If only I knew how the double-carrots properly escaped it in the first place.. :( – EvilBetty May 17 '16 at 23:40
  • `FOR /F %%i IN ('"\\Server\Shared Folder\PSEXEC.exe" \\TEST-Win7x86 cmd /c echo ^^%%PROCESSOR_ARCHITECTURE^^%%') DO set PROC=%%i` should work... – aschipfl May 17 '16 at 23:53