Print Page | Close Window

@file with user and password

Printed From: Sysinternals
Category: Sysinternals Utilities
Forum Name: PsTools
Forum Discription: PsTools questions, suggestions, comments and bug reports
URL: http://forum.sysinternals.com/forum_posts.asp?TID=11597
Printed Date: 02 September 2010 at 3:39pm


Topic: @file with user and password
Posted By: competitrack
Subject: @file with user and password
Date Posted: 31 July 2007 at 9:45am
I need to be able to run .bat files containing the net use command on a series of computers. Since the mapping listing returned by net use is specific to the user, psexec should be using the correct user and password with each machine.

Is there a way to pass -u and -p parameters using the @file along with the list of computers?



Replies:
Posted By: Karlchen
Date Posted: 31 July 2007 at 11:00am
Hello, competitrack.
Originally posted by competitrack

Is there a way to pass -u and -p parameters using the @file along with the list of computers?

The answer is a clear yes-no.

Yes, it can be done.
Provided the same adminuser/adminpass can be used to logon to all machines in e.g. computerlist.txt, then the psexec commandline will simply be:
psexec @computerlist.txt -u adminuser -p adminpass remote_command cmd_args

The computerlist.txt will look like this:
machine1
machine2
machine3
machine5
machine7


No, it cannot be done directly if the adminusers and the passwords are different on the machines inside the listfile computerlist.txt. psexec can only handle listfiles which look like the sample given above.

This is how it can be done nonetheless:
Originally posted by computerlist.txt

machine1 adminuser1 adminpass1
machine2 adminuser2 adminpass2
machine4 adminuser2 adminpass2
machine5 adminuser0 adminpass0
machine0 adminuser0 adminpass0


Originally posted by remexec.cmd

@echo off
for /F "tokens=1,2,3*" %%a in ('type computerlist.txt') do (
   echo Machine=%%a User=%%b PW=%%c Rest=%%d

   REM This will be the resulting psexec command
   psexec \\%%a -u %%b -p %%c [put in the remote command here]
)
As this is only a very rough sample, you will have to adapt it to meet your needs.

And please keep in mind:
The user given as the argument "-u user" needs to have admin privileges on the remote machine. No way around this limitation. This is by design.

Regards,
Karl


Posted By: competitrack
Date Posted: 02 August 2007 at 8:53am
Karl,

I was able to adopt your code fragment with no problem. Could you please point me to a discussion of how to direct the output of psexec net use back into the script so that I can test for the existence of a particular share on each computer? Can it be redirected to a variable or do I need to create a temporary file and read it back in or is there a more direct way?

thanks for your help


Posted By: Karlchen
Date Posted: 03 August 2007 at 7:06am
Hello, competitrack.

If I understand you right your script "remexec.cmd" looks like this now:
@echo off
for /F "tokens=1,2,3*" %%a in ('type computerlist.txt') do (
   echo Machine=%%a User=%%b PW=%%c Rest=%%d

   REM This will be the resulting psexec command
   psexec \\%%a -u %%b -p %%c net use
)


A sample output of the command net use taken on a random machine will look like this:
New connections will not be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
             A:        \\Client\A$               Client Network
OK           G:        \\win2k3srv\cde\fgh       Microsoft Windows Network
OK           H:        \\win2k3srv\homedir\karlchen
                                                 Microsoft Windows Network
OK           I:        \\win2k3srv\shared        Microsoft Windows Network
OK           T:        \\win2k3srv\temporary     Microsoft Windows Network
OK           Z:        \\win2k3srv\share4all     Microsoft Windows Network
The command completed successfully.

You may notice the following things:
+ The machine name of the machine doing the "net use" is not given inside the output
+ There are header and footer lines which you may wish to ignore
+ Sometimes a share line may be broken into two lines

So piping the output to another console command like e.g. find or findstr may or may not be sufficient depending on what you are really looking for. The modified commandline inside the script might look like this:
psexec \\%%a -u %%b -p %%c net use | find /i "\\win2k3srv\shared"
if %errorlevel%==1 (
    echo Share not found on computer %%a
) else (
    echo Share found on computer %%a
)
"\\win2k3srv\shared" is just a sample share name. Replace it by the share you are really looking for.

HTH,
Karl


Posted By: competitrack
Date Posted: 06 August 2007 at 9:40am
Karl,

My search of the output has been successful using findstr which will return the line with the string I am searching for. I am able to see if the share is mapped on the remote machine. The problem is that the errorlevel only refers to whether psexec has successfully executed and has nothing to do with the findstr search. It always comes back as 0 whether or not a string is matched.  The output from findstr still belongs to psexec and goes to the console. Any piped command belongs to psexec and is executed remotely. Redirection of the output can only be to a file since any attempt to set a variable is interpreted by psexec and causes it to error.  My goal is to unmount the share if it is found.

FOR /F "tokens=1,2,3 delims=:" %%a IN (computerlist.txt) DO ( psexec -l \\%%a -u "comptrk\%%b" -p %%c net use | findstr /I "\\Emcfileserver\scanner_08"
)

Unavailable  Z:        \\Emcfileserver\scanner_08
net exited on coder26 with error code 0.

thanks again


Posted By: competitrack
Date Posted: 06 August 2007 at 11:52am
Karl,

The "if error level" construct works when testing the %errorlevel% variable does not. Not sure why %errorlevel% does not convey the exit status of the findstr command that proceeds it.

FOR /F "tokens=1,2,3 delims=:" %%a IN (computerlist.txt) DO ( psexec -l \\%%a -u "comptrk\%%b" -p %%c net use | findstr /I "\\Emcfileserver\scanner_08"
if errorlevel 1 (echo emc is not mounted
) else (echo issue net use /delete command
)
)

Thanks for your patience and input.


Posted By: Karlchen
Date Posted: 06 August 2007 at 2:24pm
Hi, competitrack.

Please, be assured that I tested my sample commands given http://forum.sysinternals.com/forum_posts.asp?TID=11597&PID=52690#52690 - here before posting them. In particular I made sure that on my system the errorlevel I queried was the errorlevel of the "find.exe" command and not the errorlevel of "psexec.exe".
I did not verify, however, if findstr will always return 0 as its errorlevel. I can assure you "find.exe" returns 0 if the search string has been found, it returns 1 if the search string has not been found.
Any piped command belongs to psexec and is executed remotely.
Nope.
The "if error level" construct works when testing the %errorlevel% variable does not.
The availability of the "%errorlevel%" variable depends on the Windows version. It should be available at minimum from Win2K upwards.
Maybe the issue really is that "findstr" does not only return 0 and 1, but other values as well.
Originally posted by competitrack

if errorlevel 1
will be true for any errorlevel equal to or greater than 1.
Originally posted by karlchen

if %errorlevel%==1
will be true only if the return code is exactly 1.
So the 2 "if conditions" are not fully equivalent.

Anyway, good to learn you achieved your goal and psexec does what you expect it to do.

Kind regards,
Karl


Posted By: competitrack
Date Posted: 07 August 2007 at 5:16am
Karl,

I placed an echo %errorlevel% immediately following the findstr command  and for each iteration of the FOR loop, findstr always returned 0 whether the string was found or not.  Why it worked that way on my system is an open question but "if errorlevel" does the job.

I agree that the important thing is that psexec now does what I need it to do and will be proceeding with further testing of the net use command to disconnect shares remotely.  psexec is certainly an important utility and deserves the excellent support you are giving it.

thanks again


Posted By: Karlchen
Date Posted: 07 August 2007 at 6:51am
Hi, competitrack.
I placed an echo %errorlevel% immediately following the findstr command
This is a delayed expansion issue:
findstr /I "\\Emcfileserver\scanner_08" logfile & echo %errorlevel%
=> cmd.exe reads the complete line and determines the value of %errorlevel% before the findstr is executed. %errorlevel% will be 0.
findstr /I "\\Emcfileserver\scanner_08" logfile
echo %errorlevel%
=> cmd.exe reads the first line, executes it, then reads the 2 line and evaluates %errorlevel%. Now it will really hold the exit code of the findstr command.

You may change the default behaviour of "immediate variable expansion2 to the behaviour you expect, "delayed expansion" by putting this command, setlocal ENABLEDELAYEDEXPANSION, at the top of your script file e.g. (cf. http://www.microsoft.com/technet/prodtechnol/Windows2000serv/support/FAQW2KCP.mspx - here , please. Click on the question "What is delayed environment variable expansion?".)

Regards,
Karl



Posted By: molotov
Date Posted: 07 August 2007 at 7:03am
Hope nobody minds if I chime in... Embarrassed
 
Gil poses one option for controlling delayed expansion http://forum.sysinternals.com/forum_posts.asp?TID=10955#48908 - here .
 
Also, the help for CMD.EXE (/?) indicates that the params /V:ON and /V:OFF enable or disable delayed expansion for the specified instance of the command interpreter.
 
Finally, some registry settings can also control delayed expansion:
[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion]    [HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion]
DelayedExpansion is a REG_DWORD value, where it would seem a value of 0 would disable delayed expansion, and 1 would enable it.
 
More info about syntax, and precedence of the various options for controlling delayed expansion, are available in the CMD /? help screens.


-------------
Daily affirmation:
net helpmsg 4006


Posted By: Karlchen
Date Posted: 07 August 2007 at 7:28am
Nobody will mind.
Yes, a lot of roads lead to Rome (as we ancient Romans keep on saying.)

Karl



Print Page | Close Window