VBscript无法运行Shell命令

时间:2012-10-25 14:25:24

标签: vbscript wsh

我已经设置了以下Sub来快速轻松地运行shell命令。 此脚本适用于我公司的登录脚本。 我目前正在开发一个脚本,以便将大量用户添加到我们的域中。 当我在我的新脚本中使用此Sub时,我收到一条错误消息,指出无法找到该文件。 我已尝试在此stackoverflow帖子中使用此修复程序,但即使使用此代码我也收到相同的错误。 VBScript WScript.Shell Run() - The system cannot find the file specified

我觉得令人费解的部分是,当从域控制器的netlogon文件夹运行时,这个子工作正常。 我做错了什么?

谢谢,

Sub runcommand(strCommand)
Dim objWshShell, intRC
set objWshShell = WScript.CreateObject("WScript.Shell")
intRC = objWshShell.Run(strCommand, 0, TRUE)
call reportError(intRC,strCommand)
set objWshShell = nothing 
end Sub

function reportError(intRC, command)
if intRC <> 0 then 
 WScript.Echo "Error Code: " & intRC 
 WScript.Echo "Command: " & command 
end if
end function

1 个答案:

答案 0 :(得分:1)

strCommand的先前值没有空格,非常简单。您的新脚本将更复杂的变量传递给您的Sub,因此您需要额外的条件处理,正如Alex K.在上面的Collusion(即“注释/解决方案”)中指出的那样。 Alex K.的上述示例非常完美,因此,今晚作为Point Pimp,将其作为解决方案发布:

objWshShell.Run("cmd /k echo Hello World", 1, TRUE)