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.

Community
  • 1
  • 1
afterbyrner
  • 23
  • 2
  • 4
  • 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. – Jan Chrbolka Jan 13 '15 at 23:01

2 Answers2

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
8t'
  • 11
  • 1
  • 2
  • The WorkingDirectory property only applies to VBscript. For Javascript, use the CurrentDirectory property. – Kidquick Oct 27 '16 at 15:30
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);
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328