简体   繁体   English

VBScript:使用参数重定向命令行输出

[英]VBScript: redirecting output from command line with arguments

I am trying to get the "process list" from mysql and output it into a file for logging purposes. 我试图从mysql获取“进程列表”,并将其输出到文件中以进行日志记录。 here is the VBScript code: 这是VBScript代码:

 Const ForReading = 1, ForWriting = 2, ForAppending = 8
 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
 Dim fso, ts, fileObj, TextLine

 Set fso = CreateObject("Scripting.FileSystemObject")
 FileName = "mysqlprocess.log"

 If Not(fso.FileExists(FileName)) Then
    'File does not exist'
fso.CreateTextFile FileName
 End If

 'Obtain a file object for the file'
  Set fileObj = fso.GetFile(FileName)

 ' Open a text stream for output.
 Set ts = fileObj.OpenAsTextStream(ForAppending, TristateUseDefault)

 ' Write to the text stream.
 ts.WriteLine Date & " - " & Time
 ts.WriteLine

 Set objShell = WScript.CreateObject("WScript.Shell")
 'comspec = objShell.ExpandEnvironmentStrings("%comspec%")'

 Set objExec = objShell.Exec("C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql -u root -ppassword mydatabase -t -e 'show processlist;'")
 Do
     line = objExec.StdOut.ReadLine()
     strOutput = strOutput & line & vbcrlf
 Loop While Not objExec.Stdout.atEndOfStream

 ts.WriteLine strOutput

 ts.WriteLine "=============================================="
 ts.Close

And here is what is written into the mysqlprocesslist.log file: 这是写入mysqlprocesslist.log文件的内容:

5/06/2013 - 1:08:58 PM 2013/5/6-下午1:08:58

C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql Ver 14.14 Distrib 5.5.15, for Win64 (x86) Copyright (c) 2000, 2010, Oracle and/or its affiliates. C:\\ Program Files \\ MySQL \\ MySQL Server 5.5 \\ bin \\ mysql Ver 14.14 Distrib 5.5.15,适用于Win64(x86)版权所有(c)2000、2010,Oracle和/或其分支机构。 All rights reserved. 版权所有。

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Oracle是Oracle Corporation和/或其分支机构的注册商标。 Other names may be trademarks of their respective owners. 其他名称可能是其各自所有者的商标。

Usage: C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql [OPTIONS] [database] -?, --help Display this help and exit. 用法:C:\\ Program Files \\ MySQL \\ MySQL Server 5.5 \\ bin \\ mysql [选项] [数据库]-?, --help显示此帮助并退出。 -I, --help Synonym for -? -I,--help是-的同义词? --auto-rehash Enable automatic rehashing. --auto-rehash启用自动重新哈希。 One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. 不需要使用“重新哈希”来完成表和字段,但是启动和重新连接可能需要更长的时间。 Disable with --disable-auto-rehash. 使用--disable-auto-hash禁用。 (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. (默认为on;使用--skip-auto-rehash禁用。)-A,--no-auto-rehash不自动进行哈希处理。 One has to use 'rehash' to get table and field completion. 必须使用“重新哈希”来获得表和字段的完成。 This gives a quicker start of mysql and disables rehashing on reconnect. 这样可以更快地启动mysql,并在重新连接时禁用重新哈希。 [.............] [.............]

So this is working as if it was not reading out the arguments. 因此,这就像没有读取参数一样。 I tried to change the Exec line to include spaces, but this didn't work either: 我试图将Exec行更改为包含空格,但这也不起作用:

 Set objExec = objShell.Exec("C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql" & " -u root -ppassword mydatabase -t -e 'show processlist;'")

Is there anything I am doing wrong here? 我在这里做错什么吗?

I got it right, the problem was with the single quote syntax: 我说对了,问题出在单引号语法上:

Set objExec = objShell.Exec("C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql -u root -ppassword mydatabase -t -e 'show processlist;'")

The correct one is: 正确的是:

Set objExec = objShell.Exec("C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql -u root -ppassword mydatabase -t -e ""show processlist;""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM