没有得到正确的错误代码

时间:2014-12-14 19:46:05

标签: vbscript plink

当我从vbscript下面的代码运行时总是我在日志文件中得到错误状态为0而不管错误代码如何但是当我从命令提示符运行相同的代码时我得到错误代码127.下面是vbscript代码< / p>

set objShell = WScript.CreateObject ("WScript.Shell")

str = "plink.exe -ssh user@host -pw gbG8qs4D /remote_linux_server/listoffiles.sh > C:\count_data_csv.txt"

error = "error %ErrorLevel%" >> C:\count_data_csv.txt"

objShell.run  "cmd /k " & str & "&" & error

Set objShell = Nothing

请告诉我为什么错误代码存在差异

但是在运行上面的vbscript命令之后如果我在命令提示符下检查错误代码我得到错误代码127这是正确的但我的vbscript无法在count_data_csv.txt中重定向相同的错误代码:(

2 个答案:

答案 0 :(得分:2)

  1. 将保留字error用于变量,替换为Xerror
  2. cmd /V:ON ...启用延迟展开,!ErrorLevel!代替%errorlevel%
  3. 缺少echo

  4. set objShell = WScript.CreateObject ("WScript.Shell")
    str = "plink.exe -ssh user@host -pw gbG8qs4D /remote_linux_server/listoffiles.sh > C:\count_data_csv.txt"
    Xerror = "echo error !ErrorLevel!>>C:\count_data_csv.txt"
    objShell.run  "cmd /V:ON /k " & str & "&" & Xerror
    Set objShell = Nothing
    

答案 1 :(得分:0)

在多个命令中使用它时,

errorlevel尚未设置:

DOS C:\Documents and Settings\eh
echo %errorlevel%
0 <--- all is well

DOS C:\Documents and Settings\eh
dur *.* & echo %errorlevel%
'dur' is not recognized as an internal or external command,
operable program or batch file.  <--- something fishy happenend
0 <--- errorlevel still looks good

DOS C:\Documents and Settings\eh
echo %errorlevel%
9009  <--- better late then never

根据docs,当bWaitOnReturn / 3rd参数设置为True时,.Run返回被调用程序的退出代码:

  

以下VBScript代码执行相同的操作,除了它指定   窗口类型,等待用户关闭记事本,以及   保存在关闭时从记事本返回的错误代码。

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)

所以试试这本书适合你和plink。

<强> P.S。提高你的希望:

>> Set oWS = CreateObject("WScript.Shell")
>> iRet = oWS.Run("plink -ssh -pw Geheim1 eh@uw ""pipapo -l tmp.txt""", 0, True)
>> WScript.Echo iRet
>>
127