为什么 VBS 不正确 运行 ipconfig /all?

Why isn't VBS properly running ipconfig /all?

今天我带着一段有问题的代码来找你。

Set mac = CreateObject("WScript.Shell").Exec("cmd.exe /C ipconfig /all")
Do While mac.Status = 0
  WScript.Sleep 100
Loop 
MsgBox "Your MAC address is " & vbNewLine & Left(Right(mac.StdOut.ReadAll, 775),756), vbExclamation, "Physical Address"
Set mac = Nothing

我的目标是找到计算机的物理地址,并 return 使用 msgbox;
然而,在 运行 这个程序中我遇到了两件事:

  1. 即使我在命令中添加 /C,CMD 程序也没有关闭。
  2. 仔细检查后,我发现(使用 Len() 函数)在 CMD ipconfig /all 中计算了超过 6000 个字符,但是对于这个程序,它表明命令 returned 只有 4000 个字符。

我确实知道 Set mac = CreateObject("WScript.Shell").Exec("cmd.exe /C ipconfig /all") 有问题,但即使在检查了其他答案等之后我也不知道。

我建议您忘记 运行 ipconfig.exe 然后尝试解析所有返回的文本以检索 MACAddress,而是使用 WMI

示例:

Set WMI = GetObject("winmgmts:\.\root\cimv2")
Set NIC = WMI.ExecQuery("Select MACAddress " & _
  "From Win32_NetworkAdapter " & _
  "Where MACAddress Is Not NULL " & _
  "And NetEnabled='TRUE' " & _
  "And PhysicalAdapter='TRUE'") 
For Each Adapter In NIC
  MsgBox "Your MAC Address is:" & vbNewLine & _
  Adapter.MACAddress, vbInformation, "Physical Address"   
Next