我必须在命令提示符下ping并将输出保存到excel工作簿

时间:2017-03-01 07:36:44

标签: vbscript

我必须在命令提示符下ping并将输出保存到excel工作簿。如何通过 vbscript 将命令提示数据保存到excel工作簿中?我有文本文件的代码:

 Dim oShell,fs
 Set oShell = WScript.CreateObject ("WScript.Shell")
 oShell.run "cmd.exe /C ping 192.168.1.2"& str & " > log.txt"

set objLog = fs.OpenTextFile("myLogFile.txt", 8, true, 0)
Set oShell = Nothing

2 个答案:

答案 0 :(得分:0)

您不必创建存储Ping结果的临时日志文件。您可以轻松使用.Exec方法从控制台读取。示例代码如下。

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("cmd.exe /c ping 192.168.1.2")
strOut = objExec.StdOut.ReadAll()
Set objExec = Nothing : Set objShell = Nothing

Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
objExcel.Visible = False
Set Sheet1 = objExcel.ActiveWorkbook.Worksheets(1)
arr = Split(strOut, vbNewLine)
For i = LBound(arr) To UBound(arr)
    Sheet1.Range("A" & i+1).Value = arr(i)
Next
objExcel.ActiveWorkbook.SaveAs "C:\Users\pankaj.jaju\Desktop\PingResult.xlsx"
objExcel.ActiveWorkbook.Close False
Set Sheet1 = Nothing : Set objExcel = Nothing

msgbox "done"
wscript.quit

答案 1 :(得分:-1)

@echo off

SET mm=%date:~4,2%

SET dd=%date:~7,2%

SET yy=%date:~12,2%

SET hh=%time:~0,2%

SET min=%time:~3,2%

SET ss=%time:~6,2%

ping -n 4 **IP ADDRESS HERE** | findstr /r /c:"[0-9] *ms"

if %errorlevel% == 0 (

  echo **DEVICE NAME HERE**,**IP ADDRESS HERE**,%date%,%hh%_%min%_%ss%,SUCCESS > .\output.csv

) else (

  echo **DEVICE NAME HERE**,**IP ADDRESS HERE**,%date%,%hh%_%min%_%ss%,FAILURE > .\output.csv

)

回声。

您可以使用此代码一次 ping 多个 IPS 并将输出保存为:带有 ping 时间和日期的“output.csv”