Jump to content

Script to rename Network Connection


jeff.sadowski

Recommended Posts

I recently installed openvpn on my server and one client. I wanted to setup a script to install the rest of the clients the part that is difficult is renaming the Network connection.

Is there a tool that will list the network connections and their drivers? either as command line tools or

Something that has a similar output to

|driver|------------------------------------------------|networkconnection|

|Realtek PCIe GBE Family Controller--------|Local Area Connection|

|TAP-Win32 Adapter V9|------------------------|Local Area Connection 2|

I want to create some sort of script be it batch or cscript to find the connection with the driver

TAP-Win32 Adapter V9 and rename it from its current name to tap0901

I didn't find any tools but with some registry hacking I created the following script to modify the name of the network device to what is in my client.ovpn configuration file


setlocal EnableDelayedExpansion
set "networkfile=%HOMEDRIVE%%HOMEPATH%\networks.reg"
set "driversfile=%HOMEDRIVE%%HOMEPATH%\nodes.txt"
set "openvpncfg=%ProgramFiles%\OpenVPN\config\client.ovpn"

regedit.exe /e "%networkfile%" HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}
type "%networkfile%" | findstr "NetCfgInstanceId DriverDesc" > "%driversfile%"
del "%networkfile%"

set "node="
FOR /F "tokens=1,2 delims==" %%A IN ('type "%driversfile%"') DO (
set "lastnode=!node!"
set "node=%%B"
set "test=%%A"
if [!test!] == ["DriverDesc"] (
if [!node!] == ["TAP-Win32 Adapter V9"] (
set "TAPINTERFACE=!lastnode:~1,-1!"
)
)
)
type "%openvpncfg%" | findstr dev-node > "%driversfile%"
FOR /F "tokens=1,2" %%A IN ('type "%driversfile%"') DO (
if [%%A] == [dev-node] (
set "nodename=%%B"
)
)
del "%driversfile%"

echo Windows Registry Editor Version 5.00 > "%networkfile%"
echo. >> "%networkfile%"
echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%TAPINTERFACE%

\Connection] >> "%networkfile%"
echo "Name"="%nodename%" >> "%networkfile%"

regedit.exe /s "%networkfile%"
del "%networkfile%"

Edited by jeff.sadowski
Link to comment
Share on other sites


Tip: avoid using ControlSetxxx when using scripts locally from within the running OS - use instead CurrentControlSet - this avoids a problem where nothing appears to happen because the control set in use is something other than 001 (e.g. Last Known Good has ever been invoked on that machine).

If you were running offline or remotely, then there is no "CurrentControlSet", and instead you would need to look at the data held in the REG_DWORD value named Current under the key HKEY_LOCAL_MACHINE\SYSTEM\Select, then use this (padded out to 3 digits) to open keys & read values under HKEY_LOCAL_MACHINE\SYSTEM\ControlSetxxx.

Just a heads-up in case you find the script works on most, but not all the clients ;)

Link to comment
Share on other sites

I recently installed openvpn on my server and one client. I wanted to setup a script to install the rest of the clients the part that is difficult is renaming the Network connection.

Is there a tool that will list the network connections and their drivers? either as command line tools or

This seems like an example of where WMI/WMIC may come useful.

Only partially UNrelated:

jaclaz

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
  • Recently Browsing   0 members

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