CMD/Batch get active interface name as variable

前端 未结 2 1851
猫巷女王i
猫巷女王i 2021-01-23 10:08

I\'m currently having a hard time figuring out how to get the active interface names as a variable output which can be later on used in the code. I\'ve been reading here a bit,

2条回答
  •  庸人自扰
    2021-01-23 10:24

    Because the output of WMIC has the 'ugly' line endings and the value of NetConnectionID will often have unwanted trailing spaces, I'd suggest something more like this to retrieve only connected network adapters as variables:

    @Echo Off
    Set "i=0"
    For /F "Skip=1Delims=" %%A In (
     'WMIC NIC Where "NetConnectionStatus=2" Get NetConnectionID'
    ) Do For /F "Delims=" %%B In ("%%A") Do Call :Sub %%B
    Set NIC[
    Timeout -1
    Exit/B
        :Sub
        Set/A i+=1
        Set "NIC[%i%]=%*"
    

提交回复
热议问题