将netconnectionid批量传递给netsh interface命令

时间:2014-11-19 18:41:01

标签: batch-file wmic netsh

@echo off

:: tokens = 2表示通常会返回的所有令牌,我们只需要第二个令牌 :: delims ==表示应该在有=符号的地方拆分字符串 for / f“tokens = 2 delims ==”%% A in('wmic nic where“netconnectionid like'%%'”get netconnectionid / value')do(     call:dhcp“%% A” ) 暂停 退出/ b

:DHCP ::%〜1指的是传递给函数的第一个参数,但没有引号 netsh接口设置地址%1 dhcp

1 个答案:

答案 0 :(得分:1)

你非常接近。最重要的是你需要指定用于解析字符串的分隔符。由于该wmic命令将返回类似于&#34; NetConnectionId = Local Area Connection&#34;的字符串,因此您需要指明要将字符串拆分为=而不是通常的空格。< / p>

第二件事是,当你调用一个函数时,你可以访问传递给它的参数,第一个参数为%1,第二个参数为%2,等等。

@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