Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.254Now 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, shutdownshutdown -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" ) pauseNow 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.

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!.

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.

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.

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.

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.

Razor2.3 strikes again.
=====================================
If at first you don't succeed, you're about average.M2

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?

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.

It's usually a bad idea to name a script the same name as an internal command:
dir
copy
typeor am external command:
find
xcopy
fcor a reserved word:
con
aux
nul
prn
=====================================
If at first you don't succeed, you're about average.M2

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.

"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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |