Windows IOT cmd - 删除未插入(隐藏)的以太网适配器

物联网 联网 微软视窗物联网
2021-06-10 09:48:55

在 Windows IOT 中 - 是否有通过命令行删除未插入(隐藏)以太网适配器的选项?

我的问题是,当我插入 USB-TO-ETHERNET 设备适配器时,它的名称是“以太网 2”。然后我想设置静态 ip -

netsh int ip set address "Ethernet 2" static 123.0.0.123 255.255.255.0 0.0.0.0

但是,当我拔下上述适配器并将其插入另一个 USB 端口时,它的名称是“以太网 3”。

“以太网 2”不再出现,例如在 ipconfig 中。

当我为新的(“以太网 3”)设置静态 ip 时,具有相同的细节,比如

netsh int ip set address "Ethernet 3" static 123.0.0.123 255.255.255.0 0.0.0.0

我收到错误 -

The object already exists.

我试图将“以太网 2”重置为 DHCP,但没有成功 -

netsh int ip set address "Ethernet 2" dhcp

但我收到以下错误:

The filename, directory name, or volume label syntax is incorrect.

知道如何克服这个问题吗?忘记“以太网2”的设置并将其应用于“以太网3”。

谢谢。

1个回答

我不确定 Win10 IoT 是否支持 vbscript。
它需要命令行工具“devcon.exe”,
我从更大的解决方案中复制了它,我没有检查丢失的代码。

今天我会用powershell写它。
但是你会明白的。

Set goFileSystem = CreateObject( "Scripting.FileSystemObject" )
Set oWshShell = CreateObject("WScript.Shell")
Set oWshSysEnv = oWshShell.Environment( "PROCESS" )
sTempDir   = oWshSysEnv( "TEMP" )

'Create NIC hardware information
'
'Get all NIC entries (contains present / active and hidden / inactive devices)
sNICAllLogFile       = sTempDir & "\NICAll.log"
sCommandLine = "cmd /c devcon.exe findall =net ""PCI*"" > " &  sNICAllLogFile
lRetVal = oWshShell.Run( sCommandLine, 1, True )
If 0 <> lRetVal Then
   Wscript.Quit (-5)
End If
'Get present NIC (contains only present / active devices)
sNICPresentLogFile       = sTempDir & "\NICPresent.log"
sCommandLine = "cmd /c devcon.exe find =net ""PCI*""> " &  sNICPresentLogFile
lRetVal = oWshShell.Run( sCommandLine, 1, True )
If 0 <> lRetVal Then
   Wscript.Quit (-5)
End If

'Read files with NIC information and compare present NIC with all NIC entries
Set fileAllNIC = goFileSystem.OpenTextFile( sNICAllLogFile, 1, False, 0 )
sPresentNIC = goFileSystem.OpenTextFile( sNICPresentLogFile, 1, False, 0 ).ReadAll()

Do While Not ( fileAllNIC.atEndOfStream )
   aSplitString = Split (fileAllNIC.ReadLine(), ":", -1, 1)
   strLine = Trim(aSplitString(0))
   if instr(1, strLine, "matching device", vbTextCompare) = 0 Then
      'Check if NIC entry " & strLine & " is present..."
      If instr(1, sPresentNIC, strLine, vbTextCompare) > 0 Then
         '-> NIC present. Nothing to do..."
      else
         'Delete obsolete entries
         '-> NIC not present. Removing entry..."
         sCommandLine = "cmd /c devcon.exe remove ""@" & strLine & """"
         lRetVal = oWshShell.Run( sCommandLine, 1, True )
         If 0 <> lRetVal Then
            'Error: devcon failed !
            Wscript.Quit (-5)
         End If
      end if
   end if
Loop