簡體   English   中英

嘗試運行PowerShell腳本時出現“系統找不到指定的文件”

[英]“System cannot find the file specified” when trying to run PowerShell script

我正在使用wscript.shell和ActiveX調用PowerShell腳本的菜單。 我將三個變量傳遞給wshshell.Run

  • scriptID ,它是ps1文件的名稱,
  • vmName ,它是腳本將作用於的虛擬機的名稱,以及
  • checkstate ,它是與菜單項關聯的復選框的狀態。

這是我當前無法正常工作的代碼:

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

這總是產生錯誤,指出“系統找不到指定的文件”。

我嘗試遵循此處此處提到的語法,但是沒有運氣。 兩者似乎都很有希望,但會產生與上面相同的錯誤。

我測試了將變量取出並開始運行:

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

哪個可行,但是

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

失敗。

嘗試設置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

您的代碼無效,因為JavaScript中的字符串串聯運算符是+ ,而不是&

更改此:

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

到這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM