VBS将Cmd命令结果赋值给变量。

2023-06-28,,

function Runcmd(cmd)
  Dim strText
   Set oShell = CreateObject("WScript.Shell")
    Set oExec = oShell.exec("%COMSPEC% /C "&cmd)
    
    Do While Not oExec.StdOut.AtEndOfStream
        strText = oExec.StdOut.ReadAll()
    Loop
    
    Runcmd = strtext
end function

用法很简单,例如

a=runcmd("ipconfig")

那么ipconfig的命令结果会保存在变量a中

《VBS将Cmd命令结果赋值给变量。.doc》

下载本文的Word格式文档,以方便收藏与打印。