Avatar of mvalpreda
mvalpreda
Flag for United States of America asked on

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.
Windows BatchProgrammingVB Script

Avatar of undefined
Last Comment
mvalpreda

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
NVIT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mvalpreda

ASKER
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. :)
SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
zalazar

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

mvalpreda

ASKER
Actually found a way to do it with PowerShell that doesn't
Your help has saved me hundreds of hours of internet surfing.
fblack61
Qlemo

... that doesn't???
mvalpreda

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