【问题标题】:Why isn't VBS properly running ipconfig /all? [duplicate]为什么 VBS 不能正常运行 ipconfig /all? [复制]
【发布时间】:2020-07-16 02:37:06
【问题描述】:

我今天来找你,我有一个有问题的sn-p代码。

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

我的目标是找到电脑的物理地址,并用msgbox返回;
然而,在运行这个程序时,我遇到了两件事:

  1. CMD 程序没有关闭,即使我在命令中添加了 /C。
  2. 经过仔细检查,我发现(使用 Len() 函数)在 CMD ipconfig /all 中计数超过 6000 个字符,但使用此程序,它表明该命令仅返回 4000 个字符。

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

【问题讨论】:

    标签: cmd vbscript ipconfig


    【解决方案1】:

    我建议您忘记运行 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 2022-01-06
      相关资源
      最近更新 更多