Need some help writing a script

Need some help writing a script.....trying to have the script query the computer name and then run a command based on what is the resulting computer name.

For instance, these are the computer names:
DESKTOP
LAPTOP
LAPTOP-BACKUP

If the result is DESKTOP run
route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF 37
Then quit

If the result is LAPTOP run
route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF 24
Then quit

etc with LAPTOP-BACKUP, the IF # will probably be different

This is to help script VPN connections, so there will be different files based on different networks.

I have to delete the route later, but that command is easier
route delete 192.168.1.0
So that is not computer dependent.

The super bestest thing in the entire world would be if the computer name, and IF (interface) number were tied together. The network address and mask should never change. That way if I change computers or IF changes, I make one change opposed to a bunch of files.
LVL 2
mvalpredaAsked:
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.

NVITEnd-user supportCommented:
Make 2 files:
Route_Stns.txt
DESKTOP 37
LAPTOP 24
LAPTOP-BACKUP 30

Open in new window


Route_Stns.bat
@echo off
for /f "tokens=1,2" %%a in (Route_Stns.txt) do (
  if /i "%computername%" equ "%%a" ECHO route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF %%b
)

Open in new window


In a CMD window, type
Route_Stns.bat

Open in new window


Note: This version ECHOs the result to the screen for debugging reasons. Once satisfied, remove the ECHO fronting route to run it for real

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
mvalpredaAuthor Commented:
I knew it was going to be simple for someone. I am learning a little more about this. I am going to test it on the other computers and then award points. The simplicity of this is absolutely elegant. :)
Bill PrewIT / Software Engineering ConsultantCommented:
A slight enhancement.  Adjustments made to (1) exit as soon as a match is found in the TXT file, rather than continuing to read lines from it even after a host name match was found, and (2) display a warning message if a computer name is not found in the TXT file in case that needs to be addressed?

@echo off
for /f "tokens=1,2" %%a in (Route_Stns.txt) do (
  if /i "%computername%" equ "%%a" (
    route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF %%b
    exit /b
  )
)
echo *Warning* - Workstation not found in routing table, please contact IT.
pause

Open in new window

~bp
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Another improvement: Directly search the host name.
@echo off
for /F "tokens=1-2" %%a  in ('findstr /c:"%computername%" Route_Stns.txt') do set inf=%%b
if defined inf (
  route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF %inf%
) else (
  echo *Warning* - Workstation %computernamer% not found in routing table, please contact IT.
)

Open in new window

zalazarCommented:
You could also consider to use a VBScript.
Dim arrComputer(99, 1)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName
arrComputer(0,0) = "DESKTOP"
arrComputer(0,1) = "37"
arrComputer(1,0) = "LAPTOP"
arrComputer(1,1) = "24"
arrComputer(2,0) = "LAPTOP-BACKUP"
arrComputer(2,1) = "30"

For i = 0 To UBound(arrComputer)
  If arrComputer(i,0) = "" Then Exit For
  If UCase(strComputerName) = arrComputer(i,0) Then
      Wscript.echo "Creating route for IF " & arrComputer(i,1)
      strCommand = "route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 IF " & arrComputer(i,1)
      Set objScriptExec = WshShell.Exec(strCommand)
      strResults = objScriptExec.StdOut.ReadAll
      strResults = strResults & objScriptExec.StdErr.ReadAll
      Wscript.echo strResults
      Exit For
  End If
Next

Open in new window

mvalpredaAuthor Commented:
Actually found a way to do it with PowerShell that doesn't
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
... that doesn't???
mvalpredaAuthor Commented:
Hit send too fast I guess. Found a way in Powershell that does not rely on the interface number, but can use the interface alias. That is the same across computers and makes scripting much easier.
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.