You are here

Batch files - Computer name

13 posts / 0 new
Last post
King Tut
Offline
Last seen: 13 years 1 week ago
Joined: 2006-06-15 05:47
Batch files - Computer name

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?

roamer
roamer's picture
Offline
Last seen: 15 years 2 weeks ago
Joined: 2007-02-21 16:01
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.

BuddhaChu
BuddhaChu's picture
Offline
Last seen: 5 months 3 weeks ago
Joined: 2006-11-18 10:26
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. Biggrin

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!

Lurking_Biohazard
Lurking_Biohazard's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-02-18 18:06
I See...

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

~Lurk~

wraithdu
Offline
Last seen: 11 years 6 months ago
Developer
Joined: 2007-06-27 20:22
Just like the post above.

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

echo %computername%

Lurking_Biohazard
Lurking_Biohazard's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-02-18 18:06
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~

wraithdu
Offline
Last seen: 11 years 6 months ago
Developer
Joined: 2007-06-27 20:22
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.

Lurking_Biohazard
Lurking_Biohazard's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-02-18 18:06
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~

BuddhaChu
BuddhaChu's picture
Offline
Last seen: 5 months 3 weeks ago
Joined: 2006-11-18 10:26
.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!

Lurking_Biohazard
Lurking_Biohazard's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-02-18 18:06
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~

BuddhaChu
BuddhaChu's picture
Offline
Last seen: 5 months 3 weeks ago
Joined: 2006-11-18 10:26
ah...I see

I see your point. Biggrin

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!

Lurking_Biohazard
Lurking_Biohazard's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-02-18 18:06
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~

wraithdu
Offline
Last seen: 11 years 6 months ago
Developer
Joined: 2007-06-27 20:22
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.

Log in or register to post comments