简体   繁体   English

从 Powershell 调用 VBScript ..这是正确的方法吗?

[英]calling VBScript from Powershell ..Is this right way to do it?

I have a VBscript file.我有一个 VBscript 文件。 I run this VBscript using CScript on windows 2012 server.我在 Windows 2012 服务器上使用 CScript 运行这个 VBscript。 The VBscript runs fine on the server. VBscript 在服务器上运行良好。

But I need to call this VBScript file from Powershell.但是我需要从 Powershell 调用这个 VBScript 文件。 This is what I did.这就是我所做的。

For simplicity, this is what I have in my VBscript file为简单起见,这就是我的 VBscript 文件中的内容

echo.vbs回声文件

Msgbox("hello world")

I wrote the test.ps1我写了test.ps1

    $acommand = "C:\Windows\System32\Cscript.exe C:\deleteit\echo.vbs"

    Invoke-Expression $acommand

It is the right way to run an external application and you can use the same technique if you are using command line exe's or VBS scripts.这是运行外部应用程序的正确方法,如果您使用命令行 exe 或 VBS 脚本,则可以使用相同的技术。

Personally, I would be looking to add the functionality to a PowerShell script rather then calling an external VBS script, but that's just my 2 cents worth :)就个人而言,我希望将该功能添加到 PowerShell 脚本中,而不是调用外部 VBS 脚本,但这只是我的 2 美分:)

Aside from simply running除了简单的跑步

Cscript.exe C:\deleteit\echo.vbs //nologo

There's a com object that can embed vbscript right into powershell, but it's 32-bit only.有一个 com 对象可以将 vbscript 直接嵌入到 powershell 中,但它只有 32 位。 There's a way to run jobs as 32 bit.有一种方法可以将作业作为 32 位运行。 In powershell 7 you have to use the 32-bit version.在 powershell 7 中,您必须使用 32 位版本。

start-job {
  $sc = New-Object -ComObject MSScriptControl.ScriptControl.1
  $sc.Language = 'VBScript'
  $sc.AddCode('
    Function MyFunction(byval x)
      MyFunction = 2 * x
    End Function
  ')
  
  $sc.codeobject.MyFunction(1)
  $sc.codeobject.MyFunction(2)
} -runas32 | wait-job | receive-job

Output:输出:

2
4

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

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