cmd - for loop with ping/shutdown

February 17, 2009 at 05:50:19
Specs: Windows XP/Server 2003
I've been messing around for a few days now, trying to get a for loop to work to shutdown certain PC's in certain IP ranges in our network.

I have a textfile containing IP ranges named sd.txt:
192.168.21.1 to 21.254
22.1 to 22.254

Now I have a for loop, that pings every IP adress.

ping -n 1 -w 100 %%i

if ping returns errorlevel 0, IE gets a returned package, shutdown
shutdown -s -m \\%%i -t 600 -c "Long comment here"

The problem is, nothing seems to work, I tried so many variations possible and I bet it's just one or two words here or there that should be changed, the script in it's current state is

@echo off
FOR /F "usebackq" %%I IN (sd.txt) DO (
PING -n 1 -w 100 %%I
IF !ErrorLevel! == 0 shutdown -s -m \\%%I -t 60 -c "Epic fail"
)
pause

Now I've found another topic here about ping not returning %ERRORLEVEL% and I've been messing with setlocal and those variables. Problem is this works, minus the fact shutdown is not executing.


See More: cmd - for loop with ping/shutdown

Report •


#1
February 17, 2009 at 12:55:38
Your script seems not to have "setlocal enabledelayedexpansion" so !errorlevel! will be treated as a string and not a variable.

Try:

if not errorlevel 1 ......

That way you shouldn't even need delayed expansion or dynamic !variables!.


Report •

#2
February 18, 2009 at 00:08:05
I knew about the setlocal yeah, but that eventually gave me a can not SETLOCAL thing, together with an infinite loop, but I'll try the if not errorlevel statement and see what it does.


...


Nope it doesn't, it now infinetly loops.


Report •

#3
February 18, 2009 at 02:04:21
I don't see how it could infinitely loop, it runs over a finite set(text file) and has no goto's or other flow control other than the loop itself.

Do you have privileges to shutdown the remote machines? i.e. Can you shutdown a single machine from the command line?

[edit]
By the way what was the exact setlocal error? Sounds like you machine may be locked down and without command extensions.


Report •

Related Solutions

#4
February 18, 2009 at 02:57:33
Please, please, please tell me your script is named shutdown.bat

Report •

#5
February 18, 2009 at 03:52:17
I would just like to say:

"Gah, should have thought of that one."

I think you nailed it Razor2.3


Report •

#6
February 18, 2009 at 15:40:05
It is named shutdown.bat as far as I know, or Shutdown.bat, I don't know if it's cap sensitive.

Why do you ask?


I'll see if I can fish out the setlocal error later, and yes, I do have administrating rights.


Report •

#7
February 18, 2009 at 15:45:22
It is named shutdown.bat as far as I know, or Shutdown.bat, I don't know if it's cap sensitive.
Change the name. And no, it's not case sensitive.

Report •

#8
February 18, 2009 at 16:39:48
Razor2.3 strikes again.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#9
February 19, 2009 at 00:18:45
It seems that the cause isn't lying near me then, as the script was renamed to fit in the schedule that was already tasked for the old program.

Is there any explanation to the naming I can get before I'll test it out happily?


Report •

#10
February 19, 2009 at 00:56:18
Is there any explanation to the naming I can get before I'll test it out happily?
shutdown.bat: Hey PC, run shutdown for me.
PC: Okay. Now I must look for this, "shutdown." Oh, hey! I found one: shutdown.bat! Let's run it!
Repeat until PC decides it has too many batch files running.

There are methods that affect this scenario beyond renaming, but you seemingly have to come from the DOS era to understand the logic behind them.


Report •

#11
February 19, 2009 at 01:47:11
It's usually a bad idea to name a script the same name as an internal command:
dir
copy
type

or am external command:
find
xcopy
fc

or a reserved word:

con
aux
nul
prn


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#12
February 19, 2009 at 04:13:52
I understand now, and yes it seems logical to me.

Shouldn't CMD look in system32, the default folder, for the executable first?

Anyhow, thanks for the help!. It helped a lot, I've been searching for a few days already while the solution was the naming.


Report •

#13
February 19, 2009 at 05:33:02
"Shouldn't CMD look in system32, the default folder, for the executable first?"

nope


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question