3

I'm trying to write a batch script that will fetch the netconnectionid from wmin nic and then pass each of the returned values into a netsh command that will tell the interface to get it's IP from DHCP.

Here's what I have so far (non-operational)

@echo Off
For /f "tokens=2" %%a In ('wmic nic where "netconnectionid like '%%'" get netconnectionid /value') Do (
    Call :dhcp "%%a %%b"
)
pause
exit

:dhcp
netsh interface ip set address %%b dhcp

The reason the script is needed instead of running commands for "Local Area Connection" or "Wireless Network Connection" is that this script will be ran on machines where the netconnectionid is no longer following the standard.

Being new to batch, I'm having issues deciphering the loop, and where exactly it's going wrong.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Zachariah
  • 31
  • 4

1 Answers1

2

You were very close. The biggest thing is that you needed to specify the delimiter to use to parse the string. Since that wmic command will returns strings like "NetConnectionId=Local Area Connection", you need to indicate that you want to split the string at the = instead of the spaces like it ordinarily would be.

The second thing is that when you call a function, you can access the parameters that are passed into it with %1 for the first parameter, %2 for the second parameter, etc.

@echo off

:: tokens=2 indicates that of all the tokens that would normally be returned, we only want the second one
:: delims== indicates that the string should be split up wherever there is an = symbol
for /f "tokens=2 delims==" %%A in ('wmic nic where "netconnectionid like '%%'" get netconnectionid /value') do (
    call :dhcp "%%A"
)
pause
exit /b

:dhcp
:: %~1 refers to the first parameter passed into the function, but without the quotation marks
netsh interface set address %1 dhcp
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • I'm afraid that this returns the following error: Invalid source parameter (Area) – Zachariah Nov 19 '14 at 20:00
  • Perhaps the quotation marks were necessary after all. Remove the `~` character from the last line and see if that works. – SomethingDark Nov 19 '14 at 20:02
  • Also, `netsh interface /?` is telling me that ip isn't a valid parameter. There are ipv4 and ipv6, but you can't use those at the same time as set, so remove that as well. – SomethingDark Nov 19 '14 at 20:05
  • Ah, this seems to have corrected my issue. I knew I had to be close, but couldn't quite get it. Thank you very much. I've been using ip undeclared as ipv4 or ipv6 and it has been operating without issue on 7 and 8. It may be targeting both instead of a specific interface. One question moving forward, if a machine has multiple network adapters, would there need to be a command to run for %2 as well? – Zachariah Nov 19 '14 at 20:10
  • Right now, the `for` loop runs the dhcp on every netconnectionid it finds, so if a machine has multiple network adapters, it will run the command on all of them. `%2` would be used if you passed a second parameter to the function for some reason, but there's no need to do that with the current code. – SomethingDark Nov 19 '14 at 20:16
  • Thanks once again. That was my understanding of the code, but I find it's better to know for certain than to get unwanted surprises later on. – Zachariah Nov 19 '14 at 20:21