300+ free and legal portable apps (over 9GB), up-to-date, & new apps regularly (Jul 28, 2014: Transmission-qt).
Get the fast, new 64GB PortableApps.com Companion, encrypted PortableApps.com Carbide, or carry your KeyZ.
PA.c Platform 12.0 Beta 3.2 (May 20, 2014) | PortableApps.com needs your help: Please donate today.

Batch files - Computer name

King Tut - December 3, 2007 - 6:12am

Is there a command for use in a batch file that will let you run something only if it is a certain computer name, logon name, etc?


( categories: )

You may find a script

for it but I think the script will run only if it's an admin account.

OliverK> you don't live on a cow
IRC: It brings out the best in all of us...Especially when tired.

This is modified from a

This is modified from a script I use, but it's not the most efficient way to use the IF-ELSE command. Use Google or "if /?" on the command line for info on how to modify it.

The key is to evaluate the machine name then do an action if it's true and nothing if it's false.

Swap in %USERNAME% for...drumroll....the username. Laughing out loud

Type "set" in a command prompt to see a whole bunch of environment variables you can use.

SET TARGETMACHINE=MyComputerName

IF %COMPUTERNAME% EQ %TARGETMACHINE% (goto FoundName) ELSE (goto Error)

:FoundName
   do stuff
   Goto End

:Error
   echo "machine name not found"

:End
   exit

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

I See...

Side question:
How do you get the current computer name automatically?

~Lurk~

Just like the post above.

Just like the post above. It's an environment variable. At a cmd prompt type

echo %computername%

Yes

I mean to use at another spot in the batch, without typing it in. Maybe IP would be better instead. Some thing like:

for /f "tokens=15" %%B in ('ipconfig ^| find "IP Address"') do set IPAddress=%%B
if "%IPAddress%"=="" for /f "tokens=12" %%B in ('ipconfig ^| find "IP Address"') do set IPAddress=%%B

I wish I could find where I was reading about this again...

~Lurk~

I'm not sure I know what you

I'm not sure I know what you mean by using it without typing it in. And are you looking for the computer name or the IP address now? You can use %computername% anywhere in your batch script to insert the computer's name.

Depends

I can already get the IP range for the current network. Now I would like to exclude the host machine by name or IP, whichever is easier. I was just thinking that since I would already be pinging each IP address in the given range, it might be easier to exclude by IP rather than name. Sorry for the confusion, I hope this is clearer.

~Lurk~

.vbs > .bat

That would be done a LOT easier with a VBScript rather than a batch file.

Is this a login script? Where are you using this and what's the overall idea? That info may help us, help you better.

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

Network Shutdown

It is possible to shutdown (at least a XP) PC over a network. My idea was to automate the process to handle multiple PCs. I know there are limitations, like having to have same UN & PW.

I have succeeded, to a point. I can get the IP range, ping each IP, & send shutdown signal upon positive response. I just need to get the local IP and exclude it from shutdown.

~Lurk~

ah...I see

I see your point. Laughing out loud

This still sounds like a PITA without the use of an array or "real" looping like you would have in a scripting language (any of them). The only way I can think of doing it in a batch file would to be to split the range into two halves with localhost IP being the dividing line.

(Example assumes a subnet mask of 255.255.255.0 or /24 in CIDR so only the fourth octet "matters")

network = 192.168.1.x
localmachine = 192.168.1.78

Spliting on the local machine would give you two ranges or groups of IPs to ping.

.1 thru .77 and .79 thru .255

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

Batch

Here is a test batch I was working on many moons ago. (REMs removed) Maybe you or someone else can help me straighten this out...

@ECHO OFF

for /f "tokens=15" %%B in ('ipconfig ^| find "IP Address"') do set IPAddress=%%B
if "%IPAddress%"=="" for /f "tokens=12" %%B in ('ipconfig ^| find "IP Address"') do set IPAddress=%%B

FOR /F "TOKENS=2* DELIMS=:" %%a IN ('ipconfig ^| find "IP Address"') DO SET IP=%%a
FOR /F "DELIMS=. TOKENS=1,2,3" %%1 IN ("%IP%") DO SET IPR=%%1.%%2.%%3.
SET IPR=%IPR: =%
SET /A NUM=0

:REPEAT
IF %NUM%==256 GOTO EOF
SET /A NUM=%NUM%+1>NUL
SET IP=%IPR%%NUM%
TITLE CHECKING %IP%

IF %IPR%%NUM%==%IPAddress% GOTO REPEAT

PING %IP% -n 1 -w 100 >NUL
IF %ERRORLEVEL%==1 (ECHO %IP% NO REPLY) ELSE (GOTO DOWN)
GOTO REPEAT

:EOF
TITLE SCAN COMPLETED
ECHO.
ECHO. FINISHED!!
PAUSE>NUL

:EXIT
EXIT
EXIT

:DOWN
ECHO %IP% Shuting down...
SHUTDOWN -m %IP% -s -f -t 01
GOTO :REPEAT

~Lurk~

Well typing 'set' at a

Well typing 'set' at a command prompt will give you all sorts of useful environment variables.

%computername%
or
%logonserver%

should get you what you need. They will expand and work inside your BAT script.