0

test.bat

@echo off
for /f "delims=" %%i in ('winrs /r:REMOTE_MACHINE /u:DIFF_USER_ACCOUNT /p:PASS /noprofile "echo %PYTHONPATH%"') do set dest_dir=%%i
echo %dest_dir%

This script is meant to remote into the REMOTE_MACHINE and print out the contents of the PYTHONPATH env var. I intentionally pass the /noprofile switch since I do not want to access REMOTE_MACHINE with my account (which also exists on the box), which is the default behavior of winrs.

What ends up happening is that the script executes and remotes into the REMOTE_MACHINE using my user account, and not the DIFF_USER_ACCOUNT that was specified. The way I determine this is because the PYTHONPATH is different between the two accounts.

I've also created a new env variable that only exists on DIFF_USER_ACCOUNT and the batch script cannot access it.

Running the same command 'winrs /r:REMOTE_MACHINE /u:DIFF_USER_ACCOUNT /p:PASS /noprofile "echo %PYTHONPATH%"' in the shell, or just in the script without the subshell, provides the correct behavior.

Why does this happen, and how do I fix this?

ajoseps
  • 1,871
  • 1
  • 16
  • 29
  • The variable reference `%PYTHONPATH%` is going to be expanded on the machine that executes the batch file, no matter if it appears within a `for /F` loop or not, because `%`-expansion is one of the very first things happen, even before recognition of internal or external commands (like `winrs`). Perhaps you need to escape the `%`-signs by doubling them, but this is just a guess since I do not know what `winrs` expects… – aschipfl Sep 27 '21 at 18:33
  • it doesn't get expanded. winrs executes the command remotely and returns the definition on the remote machine correctly. the issue is that it's using the wrong user (the default) to do so when it's run with the subshell – ajoseps Sep 27 '21 at 18:35

0 Answers0