2015-01-13 56 views
0

我正在調用帶有wscript.shell和ActiveX的PowerShell腳本的菜單。我傳遞三個變量來wshshell.Run嘗試運行PowerShell腳本時「系統找不到指定的文件」

  • scriptID,這是一個PS1文件的名稱,
  • vmName,這是虛擬機的腳本將作用於的名字,
  • checkstate,這是與菜單項關聯的複選框的狀態。

這裏是我當前的非工作代碼:「該系統找不到指定的文件」

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

這總是產生一個錯誤,說明

我嘗試過以下語法herehere沒有運氣。這兩個看起來很有前途,但產生了我上面的錯誤。

我測試採取的變量出來,只是運行:

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

其工作原理,但

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

失敗。

+1

我想你已經對 '+' 答案 '和'。所以只需要注意:調用PowerShell腳本時總是嘗試使用腳本的完整路徑。 -file「c:\ tmp \ test.ps1」可以節省很多間歇性問題。 –

回答

0

您的代碼不起作用,因爲JavaScript中的字符串連接運算符是+而不是&

更改此:

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

到這一點:

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

這樣做。在VB中做了一堆測試之後,我甚至從未想過他們會成爲問題。謝謝! – afterbyrner

0

嘗試將Wshell.WorkingDirectory設置爲您的test.ps1所在的目錄。

如:

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

檢查MSDN

+0

WorkingDirectory屬性僅適用於VBscript。對於Javascript,請使用CurrentDirectory屬性。 – Kidquick

相關問題