# PsExec and Remote Environment Variables, Logging, Etc

When I run PsExec on a remote computer, I always fall short of what I want. What I would like ideally in most situations is a) a log on an admin server where each individual log has the name of each the remote computer it was generated from (e.g. COMPNAME1.log, COMPNAME2.log, etc.) or b) a log file on each remote computer with whatever name I specify.

When I try scenario (a), I use the following command.

%SystemDrive%\path\to\psexec.exe @listofcomputers.txt -u DOMAIN\username cmd /c echo TEST >> \\server.company.tld\share\%computername%.log


Problem is that it never works. All the computers just write to the log where %computername% is just the computer I execute PsExec from in my office. What I want are unique logs for each computer specific in the listofcomputers.txt that will correctly use the hostname from the remote environment variable without issue. Is that even possible? It does not seem to work for me. I tried this, and the syntax is clearly wrong.

%SystemDrive%\path\to\psexec.exe @listofcomputers.txt -u DOMAIN\username "cmd /c echo TEST >> \\server.company.tld\share\%computername%.log"


PsExec just fails saying the system file cannot be found (read: syntax fail).

As for scenario (b), it appears to be a variation of a similar problem. When I run a command like this, it does not work.

%SystemDrive%\path\to\psexec.exe @listofcomputers.txt -u DOMAIN\username "cmd /c echo %computername% >> \\server.company.tld\share\aggregated.log"


Is there something I do not understand about remote path and environment variables with PsExec on the cmd.exe console (I have not even tried the dreaded PowerShell yet). I know such things work in a batch file (cmd /c \\server.company.tld\share\runthis.bat), but is there a reason it will not work when executing commands as arguments? I always need this, and can never get it!

• No one have a clue? Might ask this at the SysInternals forum too and post back an answer. – songei2f Apr 12 '10 at 1:02

You need to escape with the caret sign ^ , otherwise cmd tries to solve the variable. for example, to get the remote computer name

psexec \\pc cmd.exe /c echo ^%computername^%


Gents, I finally made it to work:

psexec \\server cmd.exe /c echo %%computername%%

(notice the double percent signs)

Power Admin's PAExec (PsExec replacement) has optional command options to log what it is doing. Maybe that is what you're after?

Create a .cmd (or whatever) that does what you need, then -copy over to the list of machines using psexec for execution.

I realize its been a while, but it seems like no one had the right answer: use delayed variable expansion.

psexec @listofcomputers.txt -u DOMAIN\username cmd /v /c "echo TEST >> \\server.company.tld\share\!computername!.log"


I know its been even longer. I use a variation below as batch file named id.bat

@psexec \\%1 cmd.exe /c echo %%computername%% 2>nul


usage

C:\utils>id 192.168.10.23
TEXT_LAPTOP7

C:\utils>_


psexec \computer cmd.exe /c Echo %computername%

will do what you want

PS PSEXEC rocks! People who think batch script is dead don't know how to use it