NETSH Script Help

G

Guest

Ok, I have searched everywhere and can't find an answer. Anyhelp would be
greatly appreciated.

I am working on a batch file to change PCs from Static to DHCP using NETSH.
However, we have run into the problem that some of the users have changed the
name of their Network Interface. Since we do not know what they have been
changed too, we can not change them using the NETSH command (since you have
to specify the interface name.)

I am looking for one of the following:

1) An enviromental variable that reprents the Interface Name. If one
exists, we can simply specify it in the NETSH command.

2) A way to change the name of the interface using DOS batch files.

3) Another solution that can be performed remotely.

Anyhelp would be greatly appreciated.
 
G

Guest

Unfortunately, that does not solve the issue.

If you go look at Network Connections, you will see your NIC connection.
The default is named Local Area Connection. You can change the name in the
GUI by right clicking and choosing rename. I am looking for a way to rename
it in a script or restore the name to local area connection. I need to
change a mass number of PCs from Static to DHCP without touching them myself,
so by using a login script. However, some of the adapters may be renamed.
Hence the problem with NETSH; as I do not know the names. If I can find a
way to rename them, or reset it to local area connection or even find a way
to find the name easily and write it to a text file, that will solve my
problem.
 
G

Guest

Michael Smith said:
Unfortunately, that does not solve the issue.

If you go look at Network Connections, you will see your NIC connection.
The default is named Local Area Connection. You can change the name in the
GUI by right clicking and choosing rename. I am looking for a way to rename
it in a script or restore the name to local area connection. I need to
change a mass number of PCs from Static to DHCP without touching them myself,
so by using a login script. However, some of the adapters may be renamed.
Hence the problem with NETSH; as I do not know the names. If I can find a
way to rename them, or reset it to local area connection or even find a way
to find the name easily and write it to a text file, that will solve my
problem.

This is what I came up with Michael. Basically finds the interfaces name and
stores it into a variable(no need to rename the interface). This batch file
also checks to see if an errors have occured while switching to dhcp and logs
the error to a file(just change "c:\test\test.txt" in line 3 to the path you
prefer). Hope this helps...

***Lines that don't begin with 2 spaces have wrapped accidentally***

@echo off
title Setting IP Address to DHCP
set error_log="c:\test\test.txt"
for /f "usebackq delims=:" %%i in (`nbtstat -n ^| find ":"`) do set
interface=%%i & goto done
:done
netsh interface ip set address "%interface:~0,1%" dhcp
if %errorlevel%==1 goto error
goto :eof
:error
for /f "tokens=2* delims=:" %%i in ('ipconfig ^| find /i "ip address"') do
for %%j in (%%i) do set ip_address=%%j
echo %ip_address% has had the following error:>>%error_log%
netsh interface ip set address "%interface:~0,1%" dhcp>>%error_log%
echo ********************************************>>%error_log%
 
D

David Candy

C:\Program Files\Support Tools>netsh interface show interface

Admin State State Type Interface Name
 
G

Guest

That seems to do the job. Can you break the script down for me a little bit?
What are you referencing to get the interface name? I am trying to
decipher, but having a little trouble.

Thanks for your help!
 
G

Guest

***First off: "%interface:~0,1%" needs to be changed to "%interface:~0,-1%"***
my mistake... change this in lines 7 and 14

I used the nbtstat command. type in "nbtstat /?" in the command prompt for
usage.
I used nbtstat -n | find ":" which gave me the following:

C:\>nbtstat -n | find ":"
Local Area Connection:
Node IpAddress: [xxx.xxx.xxx.xxx] Scope Id: []

ran that output through a for loop, only allowing it to pass through the
loop once by breaking it with the "& goto done". Leaving us with "Local Area
Connection:", but used "usebackq delims=:" which left us with "Local Area
Connection" since the back delimiter was ":". then set the variable interface
to the output.
"%interface:~0,-1%" takes 1 character place off the ending of the variable
%interface%, because for some reason when it sets the variable it adds a
blank space at the end of it. i found that this causes some problems when
using the netsh command. Hope this clears some things up for ya...
 
G

Guest

Thanks a lot. This will help a lot. Now onto the other aspects as I build
this script. Thanks.

Royce said:
***First off: "%interface:~0,1%" needs to be changed to "%interface:~0,-1%"***
my mistake... change this in lines 7 and 14

I used the nbtstat command. type in "nbtstat /?" in the command prompt for
usage.
I used nbtstat -n | find ":" which gave me the following:

C:\>nbtstat -n | find ":"
Local Area Connection:
Node IpAddress: [xxx.xxx.xxx.xxx] Scope Id: []

ran that output through a for loop, only allowing it to pass through the
loop once by breaking it with the "& goto done". Leaving us with "Local Area
Connection:", but used "usebackq delims=:" which left us with "Local Area
Connection" since the back delimiter was ":". then set the variable interface
to the output.
"%interface:~0,-1%" takes 1 character place off the ending of the variable
%interface%, because for some reason when it sets the variable it adds a
blank space at the end of it. i found that this causes some problems when
using the netsh command. Hope this clears some things up for ya...


Michael Smith said:
That seems to do the job. Can you break the script down for me a little bit?
What are you referencing to get the interface name? I am trying to
decipher, but having a little trouble.

Thanks for your help!
 
G

Guest

Your welcome....

Michael Smith said:
Thanks a lot. This will help a lot. Now onto the other aspects as I build
this script. Thanks.

Royce said:
***First off: "%interface:~0,1%" needs to be changed to "%interface:~0,-1%"***
my mistake... change this in lines 7 and 14

I used the nbtstat command. type in "nbtstat /?" in the command prompt for
usage.
I used nbtstat -n | find ":" which gave me the following:

C:\>nbtstat -n | find ":"
Local Area Connection:
Node IpAddress: [xxx.xxx.xxx.xxx] Scope Id: []

ran that output through a for loop, only allowing it to pass through the
loop once by breaking it with the "& goto done". Leaving us with "Local Area
Connection:", but used "usebackq delims=:" which left us with "Local Area
Connection" since the back delimiter was ":". then set the variable interface
to the output.
"%interface:~0,-1%" takes 1 character place off the ending of the variable
%interface%, because for some reason when it sets the variable it adds a
blank space at the end of it. i found that this causes some problems when
using the netsh command. Hope this clears some things up for ya...


Michael Smith said:
That seems to do the job. Can you break the script down for me a little bit?
What are you referencing to get the interface name? I am trying to
decipher, but having a little trouble.

Thanks for your help!


:



:

Unfortunately, that does not solve the issue.

If you go look at Network Connections, you will see your NIC connection.
The default is named Local Area Connection. You can change the name in the
GUI by right clicking and choosing rename. I am looking for a way to rename
it in a script or restore the name to local area connection. I need to
change a mass number of PCs from Static to DHCP without touching them myself,
so by using a login script. However, some of the adapters may be renamed.
Hence the problem with NETSH; as I do not know the names. If I can find a
way to rename them, or reset it to local area connection or even find a way
to find the name easily and write it to a text file, that will solve my
problem.

:

Try run,cmd,in cmd type:netsh Winsock reset

:

Ok, I have searched everywhere and can't find an answer. Anyhelp would be
greatly appreciated.

I am working on a batch file to change PCs from Static to DHCP using NETSH.
However, we have run into the problem that some of the users have changed the
name of their Network Interface. Since we do not know what they have been
changed too, we can not change them using the NETSH command (since you have
to specify the interface name.)

I am looking for one of the following:

1) An enviromental variable that reprents the Interface Name. If one
exists, we can simply specify it in the NETSH command.

2) A way to change the name of the interface using DOS batch files.

3) Another solution that can be performed remotely.

Anyhelp would be greatly appreciated.

This is what I came up with Michael. Basically finds the interfaces name and
stores it into a variable(no need to rename the interface). This batch file
also checks to see if an errors have occured while switching to dhcp and logs
the error to a file(just change "c:\test\test.txt" in line 3 to the path you
prefer). Hope this helps...

***Lines that don't begin with 2 spaces have wrapped accidentally***

@echo off
title Setting IP Address to DHCP
set error_log="c:\test\test.txt"
for /f "usebackq delims=:" %%i in (`nbtstat -n ^| find ":"`) do set
interface=%%i & goto done
:done
netsh interface ip set address "%interface:~0,1%" dhcp
if %errorlevel%==1 goto error
goto :eof
:error
for /f "tokens=2* delims=:" %%i in ('ipconfig ^| find /i "ip address"') do
for %%j in (%%i) do set ip_address=%%j
echo %ip_address% has had the following error:>>%error_log%
netsh interface ip set address "%interface:~0,1%" dhcp>>%error_log%
echo ********************************************>>%error_log%
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top