首页  编辑  

脚本中运行命令并获取输出

Tags: /计算机文档/脚本,批处理/   Date Created:

在脚本中运行一个命令,并获得命令的输出结果。

dim shell, exec

set Shell = WScript.CreateObject ("WSCript.shell")

set Exec = Shell.Exec("your command")

Do While Exec.Status = 0

 WScript.Sleep 100

Loop

' Exec.StdOut.ReadAll即可返回命令的所有输出信息

' Exec.Status 即返回值

下面的脚本,可以把Excel中第一个Sheet的第一列(服务器名),运行nslookup后结果存入第二列

' Check command line switch

if WScript.Arguments.Count = 0 then

 msgbox "Usage: test.vbs [Filename]", , "Usage"

 WScript.Quit

end if

dim fs, exec

set Shell = WScript.CreateObject ("WSCript.shell")

set fso = CreateObject("Scripting.FileSystemObject")

set app = createobject("Excel.Application")

set book = app.Workbooks.Open(fso.GetAbsolutePathName(WScript.Arguments(0)))

' Scan all rows, the first column is sitename, e.g.: www.google.com

for each row in book.sheets(1).usedrange.rows

 ' run nslookup command and save the result to a temporary file

 set exec = shell.Exec("nslookup " + row.Cells(1,1))

 row.Cells(1, 2) = trim(exec.stdout.ReadAll)

next

book.save

app.visible = true