1

I am working on a menu that calls PowerShell scripts with wscript.shell and ActiveX. I am passing three variables to wshshell.Run:

  • scriptID, which is the name of a ps1 file,
  • vmName, which is the name of the virtual machine the script will act on, and
  • checkstate, which is the state of the checkbox associated with the menu item.

Here is my current non-working code:

WshShell = new ActiveXObject("Wscript.Shell");
WshShell.Run("powershell.exe  -file " & scriptID & " " & vmName & " " & checkstate, 7, true);  

This always produces an error stating "The system cannot find the file specified."

I have tried following the syntax mentioned here and here with no luck. Both seemed promising but produce the same error I have above.

I tested taking the variables out and just running:

WshShell.Run("powershell.exe -file test.ps1", 7, true)

which works, but

WshShell.Run("powershell.exe -file " & "test.ps1", 7, true)

fails.

1
  • 1
    I think you already have the answer '&' vs. '+'. So just a note: when calling PowerShell scripts always try to use a full path of the script. -file "c:\tmp\test.ps1" can save a lot of problems with intermittent issues. Commented Jan 13, 2015 at 23:01

2 Answers 2

0

Your code doesn't work, because the string concatenation operator in JavaScript is +, not &.

Change this:

WshShell.Run("powershell.exe -file " & scriptID & " " & vmName & " " & checkstate, 7, true);

into this:

WshShell.Run("powershell.exe -file " + scriptID + " " + vmName + " " + checkstate, 7, true);
Sign up to request clarification or add additional context in comments.

1 Comment

That did it. After doing a bunch of testing in VB I never even thought they would be the problem. Thanks!
0

Try setting the Wshell.WorkingDirectory to the directory where your test.ps1 is located.

eg:

wsh = new ActiveXObject("Wscript.Shell");
wsh.workingDirectory = "C:\my\dir\to\psscript";
wsh.run("powershell.exe -file test.ps1", 7, true);

Check the MSDN.

1 Comment

The WorkingDirectory property only applies to VBscript. For Javascript, use the CurrentDirectory property.

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.