Link to home
Start Free TrialLog in
Avatar of mvalpreda
mvalpredaFlag 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.
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zalazar
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

Actually found a way to do it with PowerShell that doesn't
... that doesn't???
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.