0

The following works fine:

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

When I'm trying to put the path to the script in a variabla, it's not working:

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

Script.ps1

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

How is it possible to have WScript use the variable correctly?

1
  • 1
    The second example you are missing a space after -File. Apart from that it should work fine. Commented Mar 9, 2017 at 10:14

1 Answer 1

4

Apart from a slight typo in your second example (missing space after -File) Currently, it will execute like

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

and produce an error, which you haven't documented in the question.

I can't see why this wouldn't work.

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)

It's equivalent to;

powershell.exe -ExecutionPolicy Bypass -NoLogo -File "C:\Users\bob\Downloads\Logon scripts\Script.ps1"
Sign up to request clarification or add additional context in comments.

1 Comment

You are correct, it does work. Must have been the typo. Thank you for clarifying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.