0

I have some old code in visual basic script. Instead of re-writing this old code into PowerShell, I'd like to call the VB scripts from PowerShell and capture their return value.

How can I get the return value of a visual basic script in powershell?

Something like this:

$returnValue = Invoke-Command -ScriptBlock{.\vbs\legacyVbsFunction.vbs}

The visual basic function may look like this

Function MyFunction() As Double
    Return 3.87 * 2
End Function
2
  • 2
    a) Your alleged VBScript code is not VBScript code (VBScript doesn't support typed return values). b) The return value of a function does not set the exit code of the script. c) Exit codes are integers. Floating point values are not supported. Commented Sep 4, 2019 at 9:13
  • 1
    Please create a minimal reproducible example that will allow us to reproduce the actual problem you're facing. Commented Sep 4, 2019 at 9:21

3 Answers 3

3

It sounds like you want to capture a VBScript's (stdout) output:

$output = cscript.exe //nologo .\vbs\legacyVbsFunction.vbs

Note that $output will either be a single string - if the script outputs just 1 line - or an array of strings in case of multi-line output.


For example, assuming that .\vbs\legacyVbsFunction.vbs contains the following code:

Function MyFunction
    MyFunction = 3.87 * 2
End Function

' Call the function and print it to stdout.
Wscript.Echo(MyFunction)

You could capture the output and convert it to a [double] as follows:

[double] $output = cscript.exe //nologo .\vbs\legacyVbsFunction.vbs

$output then contains 7.74.

Sign up to request clarification or add additional context in comments.

Comments

1

You can actually embed a vbscript function right in powershell using a com object called ScriptControl, but it only works in 32-bit powershell, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe: Embed VBS into PowerShell

function Call-VBScript
{
  $sc = New-Object -ComObject ScriptControl
  $sc.Language = 'VBScript'
  $sc.AddCode('
    Function MyFunction
      MyFunction = 3.87 * 2
    End Function
  ')
  $sc.CodeObject
}

$vb = Call-VBScript
$returnvalue = $vb.MyFunction()
"returnvalue is " + $returnvalue

I found out you can run a job as 32-bit:

$returnvalue = 
start-job {
  function Call-VBScript {
    $sc = New-Object -ComObject MSScriptControl.ScriptControl.1
      $sc.Language = 'VBScript'
      $sc.AddCode('
        Function MyFunction
          MyFunction = 3.87 * 2
        End Function
      ')
    $sc.CodeObject
  }

  $vb = call-vbscript
  $vb.MyFunction()
} -runas32 | wait-job | receive-job

"returnvalue is " + $returnvalue

Comments

0

You don't really have an exit code in the VBS, you have a return for a function. To actually have a return code you must close the script with something like:

wscript.quit(0) 

or

wscript.quit(returncode)

In the powershell script you must execute the vbs something like this:

(Start-Process -FilePath "wscript.exe" -ArgumentList "Script.vbs" -Wait -Passthru).ExitCode

5 Comments

Start-Process is not required. PowerShell stores the exit code of the last external command in the automatic variable $LastExitCode. You may need to run VBScripts with cscript.exe instead of the default wscript.exe, though.
Indeed, although i think it might work with wscript.exe as well.
Just tested it. wscript.exe can't be used either way, since it doesn't return the exit code (probably because it runs the script asynchronously).
Made a quick test. Created a vbscript which only has wscript.quit(101) And in a powershell script put the following command: (Start-Process -FilePath "wscript.exe" -ArgumentList "C:\Users\theje\Desktop\Tests\Teeets.vbs" -Wait -Passthru).ExitCode Worked well: PS C:\Users\theje> (Start-Process -FilePath "wscript.exe" -ArgumentList "C:\Users\theje\Desktop\Tests\Teeets.vbs" -Wait -Passthru).ExitCode 101
Ah, I had omitted the parameter -PassThru. Makes sense now. But even so, wscript.exe would only be usable with Start-Process -Wait -PassThru, so I'd still recommend using cscript.exe instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.