【问题标题】:Powershell get return value from visual basic scriptPowershell 从 Visual Basic 脚本中获取返回值
【发布时间】:2019-09-08 02:01:00
【问题描述】:

我在 Visual Basic 脚本中有一些旧代码。我不想将这个旧代码重新编写到 PowerShell 中,而是想从 PowerShell 调用 VB 脚本并捕获它们的返回值。

如何在 powershell 中获取 Visual Basic 脚本的返回值?

类似这样的:

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

visual basic函数可能是这样的

Function MyFunction() As Double
    Return 3.87 * 2
End Function

【问题讨论】:

  • a) 您声称的 VBScript 代码不是 VBScript 代码(VBScript 不支持类型化的返回值)。 b) 函数的返回值没有设置脚本的退出码。 c) 退出代码是整数。不支持浮点值。
  • 请创建一个minimal reproducible example,以便我们重现您面临的实际问题。

标签: powershell vbscript


【解决方案1】:

听起来你想捕获一个 VBScript 的(stdout)输出

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

请注意,$output 将是单个 string - 如果脚本仅输出 1 行 - 或者在 multi- 的情况下是字符串的 array输出。


对于示例,假设.\vbs\legacyVbsFunction.vbs包含以下代码:

Function MyFunction
    MyFunction = 3.87 * 2
End Function

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

您可以捕获输出并将其转换为[double],如下所示:

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

$output 然后包含7.74

【讨论】:

    【解决方案2】:

    您实际上可以使用名为 ScriptControl 的 com 对象在 powershell 中嵌入 vbscript 函数,但它仅适用于 32 位 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
    

    我发现你可以以 32 位运行作业:

    $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
    
    

    【讨论】:

      【解决方案3】:

      您在 VBS 中并没有真正的退出代码,您有一个函数的返回值。 要真正获得返回码,您必须使用以下内容关闭脚本:

      wscript.quit(0) 
      

      wscript.quit(returncode)
      

      在 powershell 脚本中,你必须像这样执行 vbs:

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

      【讨论】:

      • Start-Process 不是必需的。 PowerShell 将最后一个外部命令的退出代码存储在自动变量$LastExitCode 中。不过,您可能需要使用 cscript.exe 而不是默认的 wscript.exe 运行 VBScript。
      • 确实如此,虽然我认为它也可以与 wscript.exe 一起使用。
      • 刚刚测试过了。 wscript.exe 不能使用任何一种方式,因为它不返回退出代码(可能是因为它异步运行脚本)。
      • 进行了快速测试。创建了一个只有 wscript.quit(101) 的 vbscript 并在 powershell 脚本中输入以下命令: (Start-Process -FilePath "wscript.exe" -ArgumentList "C:\Users\theje\Desktop\Tests\Teeets.vbs " -Wait -Passthru).ExitCode 运行良好:PS C:\Users\theje> (Start-Process -FilePath "wscript.exe" -ArgumentList "C:\Users\theje\Desktop\Tests\Teeets.vbs" -Wait -Passthru).ExitCode 101
      • 啊,我省略了参数-PassThru。现在有道理了。但即便如此,wscript.exe 只能与Start-Process -Wait -PassThru 一起使用,所以我仍然建议使用cscript.exe
      猜你喜欢
      • 2022-08-04
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 2019-05-21
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多