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

LVL 2
stlhostAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
 
Mohammed KhawajaManager - Infrastructure:  Information TechnologyCommented:
@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
)
0
 
stlhostAuthor 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
0
 
NVITCommented:
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

0
 
stlhostAuthor Commented:
Perfect thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.