Batch file, net use

Dear All,

I need batch file(s) that :
# in the beginning wait for 10 seconds
# connecting to a mapped drive : \\192.168.2.5\data to z drive by : net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
# and after 60 seconds if the "net use" command is not working it will terminate the net use.

We plan to make 2 files : connect.cmd and checkconnection.bat

connect.cmd will consist :
net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd

checkconnection will consist :
sleep 10
start connection.cmd
timeout 60
IF NOT EXIST Z:\ GOTO FAILURE
GOTO END
:FAILURE
net stop "workstation"
net start "workstation"
PAUSE
:END

the problem is :
# the batch file in the beginning doesn't wait for 10 seconds. Do i use the wrong command?
# net stop workstation requires me to enter "y" manually, could i bypass this? so the batch file will run "net stop workstation" without the user enter the key "y"
# we need to terminate the net use. Cause it the server (192.168.2.5) hangs, the computer that connect to it also hangs. will the net stop workstation will do that? to terminate the net use?

Please help. Thank you.
softblessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
Sleep is not a built in command.  The easiest way without an extra EXE is to use a PING.  PING to an address that responds multiple times takes around 1 second each so:

ping -n 11 127.0.0.1 >NUL

will wait about 10 secs in place of your sleep command.

The NET STOP by itself won't ask for a Y/N unless it is having to stop another service too, this is probably "computer browser"?

if so do:

NET STOP "Computer Browser"
NET STOP "Workstation"
NET START "Workstation"
NET START "Computer Broswer"

The NET USE will just stop  when it does and I don't know how long it will take, running a test now to an address that doesn't respond here it seems it takes about 25 secs on a W2K box.

So perhaps try it with a PING -n 61 127.0.0.1 >NUL for your "timeout 60" for starters.

Other than that we can use TASKLIST or TASKILL to look for the other process being open potentially and kill it, though it will timeout in the end anyway of course.

Steve
0
softblessAuthor Commented:
hi steve,

it's working, the "PING -n 61 127.0.0.1 >NUL" give me +/- 10 seconds waiting.

and it's working to for the net stop workstation.

Could you solve the 3rd question about the net use?

The problem is if the server is off, then the net use will terminate by itself. But the problem that we have is : the server hangs, and the net use cause hangs in the client computer.

Please help. Thank you.
0
Steve KnightIT ConsultancyCommented:
Change connection.cmd by adding
exit
to the end.
then we can use

tasklist /fi "IMAGENAME EQ net.exe"  find "net.exe"
which will return %errorlevel% 0 when net.exe is running and 1 if not

Then we can do a
taskkill /IM net.exe /F

Frankly you could just do a taskkill anyway I suppose so either:

ping -n 61 127.0.0.1 >Nul
tasklist /fi "IMAGENAME EQ net.exe"  find "net.exe"
if %errorlevel%==0 taskkill /IM net.exe /F

or just

taskkill /IM net.exe /F 2>NUL

Steve


0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Steve KnightIT ConsultancyCommented:
The other way, to make sure it is the RIGHT net.exe you are closing in case somehow there is another, change the START command to include a title, e.g.

START "killme" connection.cmd

then use

TASKKILL /FI "WINDOWTITLE EQ killme - connection.cmd"

hth

Steve
0
LeeeRussellCommented:
Another way to use ping as a timer, and get a more accurate time - is to ping a non-existent address, but set a time limit for the ping attempts in milliseconds.  So for 10 seconds you would use something like:

PING 1.1.1.1 -n 1 10000 >NUL
0
Steve KnightIT ConsultancyCommented:
Very true and I use that sometimes too, though PING by default waits 1 second so ping -n 31 for insatnce to localhost will bring:

C:\Documents and Settings\stephen>echo %time% & ping 127.0.0.1 -n 31
 8:40:48.85

Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
<snip>
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 31, Received = 31, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms

C:\Documents and Settings\stephen>echo %time%
 8:41:18.92


Which is near enough 30 secs for me :-)

It does one ping then waits a sec, then the other 30 with a second inbetween which is why you do 31.

Steve
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
To prevent the start to open a new (disturbing) Window, and make 100% sure you are waiting on the correct process, you could also do this:

set id=netuse%random%
start /b "%id%" connection.cmd
ping -n 11 127.0.0.1 >nul
taskkill /FI "WINDOWTITLE EQ netuse%random%*" /F

The called cmd needs an EXIT at the end, to terminate the additional shell started by start /b.

0
Steve KnightIT ConsultancyCommented:
Interesting Qlemo....  though I couldn't get it to find the other process, surely there isn't a window with the other title without the other box?
Steve
0
Steve KnightIT ConsultancyCommented:
Oh hang on... you used netuse%random% again so it gets another random number.  Try

set id=netuse%random%
start /b "%id%" connection.cmd
ping -n 11 127.0.0.1 >nul
taskkill /FI "WINDOWTITLE EQ %id%*" /F
but still :

C:\>p
C:\>set id=netuse14163
C:\>start /b "netuse14163" connection.cmd
C:\>ping -n 11 127.0.0.1  1>nul
C:\>net use r: \\128.127.1.23\c$
C:\>taskkill /FI "WINDOWTITLE EQ netuse14163*" /F
INFO: No tasks running with the specified criteria.
C:\>System error 53 has occurred.
The network path was not found.
C:\>exit

but works fine without the /b

Steve
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Steve,
You are right, that can't work, there is no Windows title when using start /b. Stupid me. And that double %random% use, tsss. Shouldn't post before I had several litres of coffee.

0
Steve KnightIT ConsultancyCommented:
Know the feeling :-)
0
softblessAuthor Commented:
Hi Steve and Qlemo,

I'm a bit confused with your explanations. My english is not too well.

Could I conclude that, the connection.cmd will be like this :
net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
end

and the checkconnection.bat will be like this :
ping -n 11 127.0.0.1 >Nul
start "killme" connection.cmd
ping -n 61 127.0.0.1 >Nul
tasklist /fi "IMAGENAME EQ net.exe"  find "net.exe"
if %errorlevel%==0 TASKKILL /FI "WINDOWTITLE EQ killme - connection.cmd"

Is it correct?

And btw, Qlemo suggestion would be great, could we hide to eliminate the pop up window? How? Is it using :

set id=netuse%random%
start /b "%id%" connection.cmd

Kindly advise. THank you.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Regarding hiding:
If we use start /b (which would really hide), you do not have a window title to search for, and hence nothing to kill that easy.
As a replacement I recommend to start the net use minimized (see below).

The code is not correct, it is mixing some statements up. That's the correct one (and I will not use a second batch file, instead I issue the net use directly). Further checking for the running task is not needed, we will just force kill and ignore any error message telling us it is not there any more:

@echo off
ping -n 11 127.0.0.1 >Nul
start /b "killme" net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd& exit
ping -n 61 127.0.0.1 >Nul
taskkill /FI "IMAGENAME net.exe" /F >nul 2>nul

Open in new window

0
Steve KnightIT ConsultancyCommented:
Nearly.... exit not end in the first batch and you have a | missing which pipes the output of the tasklist into find.  hth

Steve

connection.cmd:
net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
exit

checkconnection.bat:
@echo off
REM Wait 10 seconds
ping -n 11 127.0.0.1 >Nul
REM Try to map drive in different process
start "killme" connection.cmd
REM Wait 60 seconds
ping -n 61 127.0.0.1 >Nul
REM If The other process is still running then errorlevel is set to 0 and need to kill it
tasklist /fi "IMAGENAME EQ net.exe" | find "net.exe"
if %errorlevel%==0 TASKKILL /FI "WINDOWTITLE EQ killme - connection.cmd"

0
Steve KnightIT ConsultancyCommented:
BTW As Qlemo suggested (but I think posted the wrong code again maybe :-) you could add /MIN to the START line to run the net use minimised so it is less obvious if needed.

Qlemo's way of running the net use and exit on the START line is another alternative to your current second batch file if you want too.  It would look something like:

@echo off
REM Wait 10 seconds
ping -n 11 127.0.0.1 >Nul
REM Try to map drive in different process
start /MIN "killme" net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd& exit
REM Wait 60 seconds
ping -n 61 127.0.0.1 >Nul
TASKKILL /FI "WINDOWTITLE EQ killme*"

or something like that..... points split with Qlemo if you head that way of course.

Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Thanks, Steve, my code is correct, but I forgot to show how to use the /min switch as alternative. That would have looked almost like your last post, but without the &exit:

@echo off
ping -n 11 127.0.0.1 >Nul
start /MIN "killme" net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
ping -n 61 127.0.0.1 >Nul
TASKKILL /FI "WINDOWTITLE EQ killme*"

Open in new window

0
Steve KnightIT ConsultancyCommented:
True.... think I prefer your running the net use directly from START command anyway, not sure why I didn't do it myself!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.