Jump to content

Help writing variable value to Cmd / txt file.


Recommended Posts

Hi all, I am extremely new to autoit so I am sorry if this is easy.

I am trying to write a script that will allow me to change the current network connection's IP to a static address. I need this for a rollout of computers that are all configured differently (fun) so it would be annoying to have to look up this info for each of them. So far, I have a script that finds the name of the current connection as it appears in "Network and Sharing Center > Change adapter settings", whether its "Local Area Connection 1" or whatever and sets the name to a variable. The easiest way I have found to actually changing the IP address to a specific address is through command prompt, so I have several versions of creating a batch file to change the IP to static. None of them have worked so far, instead of sending "Local Area Connection 1", it sends the actual variable name.

The one that works (as far as sending the actual value of the variable): 

Global $Var1 = "1234"
EnvSet("testvar",$Var1)
Run(@ComSpec & " /k  echo netsh interface ipv4 set address name=%testvar% static 192.168.1.223 255.255.255.0 192.168.1.1")

But that doesn't actually change the ip address, its just text in a command prompt. (not sure if thats something easier to fix than the following)

My other script makes a txt file and then runs it at the end as a batch. I have the actual "turn into .bat and run" part commented out so I can read the txt file: 

Global $Var1 = "1234"
EnvSet("testvar",$Var1)

$file = FileOpen("C:Testing.txt", 9)
 
FileWriteLine($file, "@echo off")
FileWriteLine($file, "")
FileWriteLine($file, 'netsh interface ipv4 set address name= %testvar% static 192.168.0.3 255.255.255.0 192.168.0.1 1')
FileClose($file)
;FileMove("C:Testing.txt", "C:Testing.bat", 1)
 
;ShellExecute("C:\Users\Elise\Desktop\Testing.bat", "")

 

This is what appears in the txt file:

@echo off

netsh interface ipv4 set address name= %testvar% static 192.168.0.3 255.255.255.0 192.168.0.1 1

 

What I want is for it to say:  "ipv4 set address name=1234 static...." , but it is just putting in the name of the variable there lol. The other parts of the script are working and ready to go, this is the last thing I need to fix to get it working. Anyone know how to fix this? Or a better way to approach this?

 

Again, I am sorry if this is super easy. Thank you for any help!

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...