Not getting proper error code

262 Views Asked by At

When i am running below code from vbscript always i am getting error status as 0 in a log file irrespective of error code but when i run the same code from command prompt i am getting error code 127. below is the vbscript code

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

Please let me know why there is a difference in error code

But after running above vbscript command if i check error code in command prompt i am getting error code 127 which is correct but my vbscript doesn't able to redirect same error code in count_data_csv.txt :(

2

There are 2 best solutions below

1
On BEST ANSWER
  1. used reserved word error for a variable, replaced by Xerror
  2. cmd /V:ON ... enables delayed expansion which is put to use by !ErrorLevel! instead of %errorlevel%
  3. missing echo

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
0
On

errorlevel isn't set yet when you use it in a multiple command:

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

According to the docs, .Run returns the exit code of the called program when invoked with the bWaitOnReturn/3rd parameter set to True:

The following VBScript code does the same thing, except it specifies the window type, waits for Notepad to be shut down by the user, and saves the error code returned from Notepad when it is shut down.

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

So try if going by the book works for you and plink.

P.S. To raise your hopes:

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