在 WScript.Run 中使用变量

Use variable in WScript.Run

以下工作正常:

VBScript

Script = "C:\Users\bob\Downloads\Logon scripts\Script.ps1"
Dim oSHELL
Set oSHELL = CreateObject("WScript.Shell")
oSHELL.Run "powershell.exe -ExecutionPolicy Bypass -NoLogo -File ""C:\Users\bob\Downloads\Logon scripts\Script.ps1""", 0, True
Set oSHELL = Nothing

当我尝试将脚本的路径放入变量中时,它不起作用:

oSHELL.Run "powershell.exe -ExecutionPolicy Bypass -NoLogo -File ""Script""", 0, True
oSHELL.Run "powershell.exe -ExecutionPolicy Bypass -NoLogo -File""" & Script & """", 0, True

脚本.ps1

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Test MessageBox", "Information")

如何才能让 WScript 正确使用变量?

除了你的第二个例子中有一点错字 (在 -File 之后缺少 space) 目前,它会像

那样执行
powershell.exe -ExecutionPolicy Bypass -NoLogo -File"C:\Users\bob\Downloads\Logon scripts\Script.ps1"

并产生一个你没有在问题中记录的错误。

我不明白为什么这行不通。

Dim oSHELL: Set oSHELL = CreateObject("WScript.Shell")
Dim Script: Script = "C:\Users\bob\Downloads\Logon scripts\Script.ps1"
Call oSHELL.Run("powershell.exe -ExecutionPolicy Bypass -NoLogo -File """ & Script & """", 0, True)

相当于;

powershell.exe -ExecutionPolicy Bypass -NoLogo -File "C:\Users\bob\Downloads\Logon scripts\Script.ps1"