We help IT Professionals succeed at work.

Logging PSexec results

My script however some machines are not going to connect I am sure. How do I pass the results to a log file so that I can review what it missed?

@ECHO ON

set controlfile=serverlist.txt


FOR /F %%L IN (%controlfile%%) DO (
  SET "line=%%L"
  psexec \\%%L net user somesuer somepassword /add && net localgroup administrators "someuser" /add
)

Open in new window

Comment
Watch Question

Mohammed KhawajaManager - Infrastructure:  Information Technology

Commented:
@ECHO ON

set controlfile=serverlist.txt


FOR /F %%L IN (%controlfile%%) DO (
  SET "line=%%L"
echo %%L >> logfilename.txt
  psexec \\%%L net user somesuer somepassword /add && net localgroup administrators "someuser" /add >> logfilename.txt
)

Author

Commented:
Tried this method I even tried >> \\servershare\log.txt and it didnt work. Txt file shows The command completed successfully. That is all it says
NVITEnd-user support

Commented:
Try this:
@ECHO ON
set controlfile=serverlist.txt
FOR /F %%L IN (%controlfile%) DO (
  SET "line=%%L"
  psexec \\%%L net user somesuer somepassword /add 2>> logfilename.txt && net localgroup administrators "someuser" /add >> logfilename.txt
)

Open in new window

"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
1. There is double percent where it shouldn't in the FOR.
2, && is a command separator in a batch, so the second command is executed locally instead of remote.
3. Using PsExec with a more complex command can get tricky, because of need of proper escaping, and some bugs and issues with both cmd.exe and PsExec.
@ECHO ON

set controlfile=serverlist.txt

FOR /F %%L IN (%controlfile%) DO (
  SET "line=%%L"
  psexec \\%%L cmd /c "net user somesuer somepassword /add && net localgroup administrators ^"someuser^" /add"
) >> logfile.txt 2>&1

Open in new window

Author

Commented:
Perfect thanks