"System cannot find the file specified" 尝试 运行 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);
这总是会产生错误 "The system cannot find the file specified."
我曾尝试遵循 here and here 中提到的语法,但没有成功。两者看起来都很有希望,但会产生与上面相同的错误。
我测试了去掉变量后 运行:
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);
我正在开发一个菜单,该菜单使用 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);
这总是会产生错误 "The system cannot find the file specified."
我曾尝试遵循 here and here 中提到的语法,但没有成功。两者看起来都很有希望,但会产生与上面相同的错误。
我测试了去掉变量后 运行:
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);